I've trying to display the attributes from size in woocommerce.
I'm using
<?php
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<p>. $term->name .</p>";
?>
However it just seems to break the page for some reason.
Full code
<div class="titleSP">
<h2><?php the_title(); ?></h2>
<h2><?php the_title(); ?></h2>
</div>
<div class="priceSP">
<?php echo $product->get_price_html(); ?>
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
<?php
//code to pull in the attrs
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<p>. $term->name .</p>";
?>
<?php if($post->post_excerpt) { ?>
<div class="descriptionSP short">
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ); ?>
</div>
<?php } ?>
Related
I have add a new field via admin panel in products product_icon
where I put an SVG icon code. I need to use this SVG plugin. I have put into woocommerce/includes/abstracts/abstract-wc-product.php new function.
public function get_icon( $context = 'view' ) {
return $this->get_prop( 'icon', $context );
}
when I put it into a while loop where I have list products name, prices, etc new function get_icon() I have null. In SQL post meta meta key is ok "product_icon" for a correct product with the correct content (SVG code)
My listing Code:
<?php
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$product = wc_get_product( get_the_ID() );
?>
<div class="eb-woo-products-col">
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<div class="producttabwhite eb-woo-product">
<?php } else { ?>
<div class="producttabblue eb-woo-product">
<?php } ?>
<div id="mydiv" class="eb-woo-product-image-wrapper" style="margin-top:2.5rem;margin-right:1.25rem;">
<?php echo wp_kses_post( $product->get_image( 'woocommerce_thumbnail' ) ); ?>
<?php if ( 'grid' === $layout ) { ?>
<div class="eb-woo-product-overlay">
<div class="eb-woo-product-button-list">
<?php // woocommerce_template_loop_add_to_cart(); ?>
</div>
</div>
<?php } ?>
</div>
<div class="eb-woo-product-content-wrapper1">
<div class="ml40px mt33px col-sm-8">
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<p class="producttitle">
<a class="producttitle" href="<?php echo esc_url( get_permalink() ); ?>"><?php echo get_the_title(); ?></a>
</p>
<?php } else { ?>
<p class="producttitlewhite">
<a class="producttitlewhite" href="<?php echo esc_url( get_permalink() ); ?>"><?php echo get_the_title(); ?></a>
</p>
<?php } ?>
<?php if ( 'grid' === $layout && 'grid-preset-3' === $gridPreset ) { ?>
<p class="productprice">
<?php if ( $showPrice ) { ?>
<p><?php echo $product->get_price_html(); ?></p>
<p>Icon code<?php echo $product->get_icon(); ?></p>
<div style="display:block" class="mt-4 eb-woo-product-button-list"><?php woocommerce_template_loop_add_to_cart(); ?></div>
<?php } ?>
</p>
<?php } else { ?>
<p class="productpricewhite">
<?php if ( $showPrice ) { ?>
<p><?php echo $product->get_price_html(); ?></p>
<div style="display:block" class="mt-4 eb-woo-product-button-list"><?php woocommerce_template_loop_add_to_cart(); ?></div>
<?php } ?>
</p>
<?php } ?>
</div>
</div>
</div>
</div>
<?php
}
wp_reset_postdata();
} else {
?>
<p><?php _e( 'No product found', 'essential-blocks' ); ?></p>
<?php
}
?>
Well, you don't have to edit woocommerce/includes/abstracts/abstract-wc-product.php file to fetch the meta key and the get_prop function doesn't work that way that you're assuming.
You can simply use get_post_meta function to fetch your meta key value for the product.
so you just need to replace <p><?php echo $product->get_icon(); ?></p>
with the below code:
<p><?php echo get_post_meta( $product->get_id(), 'product_icon', true ); ?></p>
I have created a Custom Post Type product and for this CPT I've also created a taxonomy with the name products_types.
Now on my overview page of all the products I would like to echo out the product type that was given to the product. But I keep getting bool(false).
My code:
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$category = get_the_terms('product', 'products_types');
var_dump($category);
echo $category;
?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
Anyone can help me out here please?
You need to pass the Post ID or object in first parameter of get_the_terms(). Used get_the_ID() which return the post ID.
Example:
foreach (get_the_terms(get_the_ID(), 'products_types') as $cat) {
echo $cat->name;
}
How to print taxonomy terms of custom post type in WordPress loop?
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$terms = get_the_terms( get_the_ID(), 'products_types' );
if ( $terms && ! is_wp_error( $terms ) ) :
$category_links = array();
foreach ( $terms as $term ) {
$category_links[] = $term->name;
}
$categories = join( ", ", $category_links );
?>
<?php printf( esc_html( $categories ) ); ?>
<?php endif; ?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
I fixed all woocommerce deprecated functions except one,maybe someone can help me out.I am no professional. Thank You.
The only plugin activated is woocommerce so there is no plugin conflict problem.The wordpress is up to date so is woocommerce.
I am getting this notice,and from what I see the problem comes from page.php.
Notice: post was called incorrectly. Product properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/barbuto/page.php'), WC_Abstract_Legacy_Product->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in /home/public_html/dev/wp-includes/functions.php on line 4139
This is the code from page.php file.
get_header(); ?>
<?php if(is_front_page()){ ?>
<!-- start of banner -->
<div class="banner" style="background: url('<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>') no-repeat scroll center center transparent;">
</div>
<!-- end of banner -->
<div class="wrapper">
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'meta_key' => '_featured', 'meta_value' => 'yes', );
$loop = new WP_Query( $args );
echo "<h1>"; count($loop); echo "</h1>";
$i=1;
while ( $loop->have_posts() ) : $loop->the_post(); global $product; global $woocommerce; ?>
<div class="product_featured_img <?php echo $loop->post->ID; ?> <?php if(($loop->post->ID == '405') || ($loop->post->ID == '72')){ echo 'bottol';} ?>">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'large'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
</a>
</div>
<div class="bottol_text product_featured_content">
<h3>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>"
title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php the_title(); ?>
</a>
</h3>
<p>
<?php
//$text_length = count($product->post->post_content);
//if($text_length > 1000){
// echo substr($product->post->post_content,0,2000).'...';
/*} else { */
echo $product->post->post_content;
//}
?>
</p>
<?php if ( is_user_logged_in() ) { ?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<div class="add_box">
<a class="add_to_cart_button button product_type_simple" data-product_sku="<?php echo $product->get_sku(); ?>" data-product_id="<?php echo $loop->post->ID;?>" rel="nofollow" href="<?php echo $product->add_to_cart_url(); ?>" > add to cart </a>
</div>
<?php }else{echo '' . __('PLEASE LOGIN/REGISTER TO VIEW PRICES', 'theme_name') . '';}?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
</div>
Replace this line echo $product->post->post_content; with echo $loop->post->post_content;
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I'm trying to create a page for WordPress "category.php" from the code in this tutorial: http://scratch99.com/wordpress/wp-seo/siloing/
My current theme (Sociallyviral by Themeforest) there is no page "category.php," so I created one and added the code below:
Author's Note:
"The code should look something like the below. Note, you can’t just use this code exactly – it needs to fit in with your theme so that the classes of wrapping divs, etc match the rest of your theme and the style is picked up."
// work out category id, then see if there are any subcategories
$main_cat_ID = get_query_var( 'cat' );
$subcats = get_categories( array( 'parent' => $main_cat_ID, 'hide_empty' => 0 ) );
// if it is page 1
if ( $paged < 2 ) :
// show the category description
echo category_description();
// if there are subcategories, loop through and show them
if ( count( $categories ) > 0 ) :
foreach ( $subcats as $category ) : ?>
<div class="subcategory">
<h2><a class="subcategory-link" href="<?php echo get_category_link( $category->term_id ); ?>">
<?php echo $category->name; ?></a></h2>
<?php echo wpautop( $category->description ); ?>
</div>
<?php
endforeach;
endif;
// get sticky posts and show them if they exist
$args = array( 'category__in' => $main_cat_ID, 'include' => get_option( 'sticky_posts' ) );
$myposts = get_posts( $args );
if ( count( $myposts ) > 0 ) :
foreach ( $myposts as $value ) : ?>
<div class="sticky">
<h3>
<a href="<?php echo get_permalink( $value->ID ); ?>" rel="bookmark"
title="<?php echo $value->post_title; ?>"><?php echo $value->post_title; ?></a>
</h3>
<div class="entry-content">
<?php
// if there is an excerpt, use it, otherwise roll our own (get_the_excerpt won't work outside the loop)
if ( $value->post_excerpt ) :
echo wpautop( $value->post_excerpt );
else :
$sjc_excerpt = explode( '<!--more-->', $value->post_content );
if ( count( $sjc_excerpt ) >= 2 ) :
echo wpautop( strip_tags( $sjc_excerpt[0] ) );
else :
echo wpautop( implode ( ' ', array_slice( explode( ' ', strip_tags( $sjc_excerpt[0] ) ), 0, 45 ) ) . ' ...' );
endif;
endif; ?>
</div>
</div>
<?php
endforeach;
endif;
// pages 2 and onwards don't worry about cat desc or stickies, but link to main cat page
else : ?>
<p>For an introduction to this topic and our latest articles, please see the main
<?php single_cat_title(); ?> page.</p>
<?php
endif;
// if we have any posts in the main loop (no stickies there)
if ( have_posts() ) : ?>
<ul class="other-posts">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
// you need to replace this with whatever your theme uses!
sjc_content_nav( 'nav-below' );
// if no posts, and also no stickies and no categories, go with no results
elseif ( count( $myposts ) == 0 && count( $subcats ) == 0 ) :
get_template_part( 'no-results', 'archive' );
endif;
If necessary, below my theme file "archive.php"
<?php $mts_options = get_option(MTS_THEME_NAME); ?>
<?php get_header(); ?>
<div id="page">
<div id="content_box">
<h1 class="postsby">
<?php if (is_category()) { ?>
<span><?php single_cat_title(); ?><?php _e(" Archive", "mythemeshop"); ?></span>
<?php } elseif (is_tag()) { ?>
<span><?php single_tag_title(); ?><?php _e(" Archive", "mythemeshop"); ?></span>
<?php } elseif (is_author()) { ?>
<span><?php $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author)); echo $curauth->nickname; _e(" Archive", "mythemeshop"); ?></span>
<?php } elseif (is_day()) { ?>
<span><?php _e("Daily Archive:", "mythemeshop"); ?></span> <?php the_time('l, F j, Y'); ?>
<?php } elseif (is_month()) { ?>
<span><?php _e("Monthly Archive:", "mythemeshop"); ?>:</span> <?php the_time('F Y'); ?>
<?php } elseif (is_year()) { ?>
<span><?php _e("Yearly Archive:", "mythemeshop"); ?>:</span> <?php the_time('Y'); ?>
<?php } ?>
</h1>
<?php $j = 1; if (have_posts()) : while (have_posts()) : the_post(); $featured = get_post_meta($post->ID, 'mts_featured', true); ?>
<article class="latestPost excerpt<?php echo (($j+2) % 3 == 0) ? ' first' : ''; ?><?php echo ($j % 3 == 0) ? ' last' : ''; ?>" itemscope itemtype="http://schema.org/BlogPosting">
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
<?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?>
<?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
<?php if ($featured) : ?><div class="post-label"><i class="fa fa-star"></i> <span><?php _e('Featured','mythemeshop'); ?></span></div><?php endif; ?>
</a>
<header>
<h2 class="title front-view-title"><?php the_title(); ?></h2>
<?php if ( ! empty( $mts_options["mts_home_headline_meta"] ) ) { ?>
<div class="post-info">
<?php if( ! empty( $mts_options["mts_home_headline_meta_info"]['date']) ) { ?>
<span class="thetime updated"><i class="fa fa-calendar"></i> <span itemprop="datePublished"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . __(' ago','mythemeshop'); ?></span></span>
<?php } ?>
<?php if( ! empty( $mts_options["mts_home_headline_meta_info"]['category']) ) { ?>
<span class="thecategory"><i class="fa fa-tags"></i> <?php mts_the_category(', ') ?></span>
<?php } ?>
</div>
<?php } ?>
</header>
</article><!--.post excerpt-->
<?php $j++; endwhile; endif; ?>
<!--Start Pagination-->
<?php if (isset($mts_options['mts_pagenavigation_type']) && $mts_options['mts_pagenavigation_type'] == '1' ) { ?>
<?php mts_pagination(); ?>
<?php } else { ?>
<div class="pagination pagination-previous-next">
<ul>
<li class="nav-previous"><?php next_posts_link( '<i class="fa fa-angle-left"></i> '. __( 'Previous', 'mythemeshop' ) ); ?></li>
<li class="nav-next"><?php previous_posts_link( __( 'Next', 'mythemeshop' ).' <i class="fa fa-angle-right"></i>' ); ?></li>
</ul>
</div>
<?php } ?>
<!--End Pagination-->
</div>
<?php get_footer(); ?>
I get this error when i try to open the category page:
"Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH) in /wp-content/themes/sociallyviral/category.php on line 20"
This syntax error, are solved!
Thank you all!
Best regards!
You are missing the <?php Tag at the beginning of your file.
Without it, the first bit of code, the parser will parse the following code as php:
echo get_category_link( $category->term_id );
echo $category->name;
echo wpautop( $category->description );
endforeach;
endif;
as you can see, the first thing to reach (beside the three echo's) is endforeach;and this is where your parser throws an error.
The authors note said the following: "...it needs to fit in with your theme so that the classes of wrapping divs, etc match the rest of your theme and the style is picked up."
This means, that you need to provide the <div>'s in your code with class and style addributes, according to your and your templates needs. In detail, you are meant to apply the classes used by your template, to apply the desired layout. You should have some form of reference of the classes your template uses (probably something like this). Where you can have a look at which classes to use.
This is what the reference of bootstrap looks like - just to give you some idea of what you are after.
Put a php tag at the beginning of your code
<?php // show the category description
echo category_description();
// if there are subcategories, loop through and show them
if ( count( $categories ) > 0 ) :
foreach ( $subcats as $category ) : ?>
<div class="subcategory">
<h2><a class="subcategory-link" href="<?php echo get_category_link( $category->term_id ); ?>">
<?php echo $category->name; ?></a></h2>
<?php echo wpautop( $category->description ); ?>
</div>
<?php
endforeach;
endif;
// get sticky posts and show them if they exist
$args = array( 'category__in' => $main_cat_ID, 'include' => get_option( 'sticky_posts' ) );
$myposts = get_posts( $args );
if ( count( $myposts ) > 0 ) :
foreach ( $myposts as $value ) : ?>
<div class="sticky">
<h3>
<a href="<?php echo get_permalink( $value->ID ); ?>" rel="bookmark"
title="<?php echo $value->post_title; ?>"><?php echo $value->post_title; ?></a>
</h3>
<div class="entry-content">
<?php
// if there is an excerpt, use it, otherwise roll our own (get_the_excerpt won't work outside the loop)
if ( $value->post_excerpt ) :
echo wpautop( $value->post_excerpt );
else :
$sjc_excerpt = explode( '<!--more-->', $value->post_content );
if ( count( $sjc_excerpt ) >= 2 ) :
echo wpautop( strip_tags( $sjc_excerpt[0] ) );
else :
echo wpautop( implode ( ' ', array_slice( explode( ' ', strip_tags( $sjc_excerpt[0] ) ), 0, 45 ) ) . ' ...' );
endif;
endif; ?>
</div>
</div>
<?php
endforeach;
endif;
// pages 2 and onwards don't worry about cat desc or stickies, but link to main cat page
else : ?>
<p>For an introduction to this topic and our latest articles, please see the main
<?php single_cat_title(); ?> page.</p>
<ul class="other-posts">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
// you need to replace this with whatever your theme uses!
sjc_content_nav( 'nav-below' );
// if no posts, and also no stickies and no categories, go with no results
elseif ( count( $myposts ) == 0 && count( $subcats ) == 0 ) :
get_template_part( 'no-results', 'archive' );
endif;
your problem will solved.
I'm currently playing around with WOOCOMMERCE V2.0.13 and I'm trying to display each product from the current product category (e.g. Construction Products when on the Construction Page), I've managed to display the single products from within the current category but if the product is also in another category (e.g Construction and Enviroment) then the current category breaks and shows zero products either from Construction or Enviroment.
If I could get some advice/help on displaying products from the current category and allow it to work with products that are in multiple categories I'd trully apreciate the help and time.
I'm more than happy to recode this entire section to make it work, here is my code below please let me know if I've missed anything.
Thank you.
<ul class="products">
<?php
global $post, $product;
$categ = $product - > get_categories();
$categ2 = preg_replace('/<a href=\"(.*?)\">(.*?)<\/a>/', "\\2", $categ);
?>
<?php
global $product;
$args = array('post_type' = > 'product', 'posts_per_page' = > '999', 'product_cat' = > $categ2, );
$loop = new WP_Query($args);
while ($loop - > have_posts()): $loop - > the_post();
global $product;
?>
<li>
<a href = "<?php echo get_permalink(); ?>">
<?php
if (has_post_thumbnail()) {
$image = get_the_post_thumbnail($post - > ID, apply_filters('single_product_large_thumbnail_size', 'shop_single'));
$image_title = esc_attr(get_the_title(get_post_thumbnail_id()));
$image_link = get_permalink($product_id);
$attachment_count = count($product - > get_gallery_attachment_ids());
echo apply_filters('woocommerce_single_product_image_html', sprintf('%s', $image_link, $image_title, $image), $post - > ID);
} else {
echo apply_filters('woocommerce_single_product_image_html', sprintf('<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src()), $post - > ID);
} ?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php
if ($price_html = $product - > get_price_html()) {
?>
<span class = "price">
<?php echo $price_html; ?>
</span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt, 15);
?>
</p>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php if ( have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<div class="courses-main">
<ul class="products">
<?php while ( have_posts() ) : the_post(); ?>
<li>
<a href="<?php echo get_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
$image = get_the_post_thumbnail( $post->ID, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ) );
$image_title = esc_attr( get_the_title( get_post_thumbnail_id() ) );
$image_link = get_permalink( $product_id );
$attachment_count = count( $product->get_gallery_attachment_ids() );
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_title, $image ), $post->ID );
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="Placeholder" />', woocommerce_placeholder_img_src() ), $post->ID );
}
?>
</a>
<div>
<h3>
<?php the_title();?>
<span>
<?php if ( $price_html = $product->get_price_html()) { ?>
<span class="price"><?php echo $price_html; ?></span>
<?php } ?>
</span>
</h3>
</div>
<div>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,15);
?>
</p>
</div>
</li>
<?php endwhile;?>
</ul>
</div>
<?php woocommerce_product_loop_end(); ?>
<?php endif; ?>