admin管理员组文章数量:1305140
I am running a woocommerce shop and I have products which are in multiple categories. I would like to show the category images on the single product pages. And also on the product overview page.
I only found how I can show the category image on the product category page:
<?php
if (is_product_category()){
global $wp_query;
// get the query object
$cat = $wp_query->get_queried_object();
// get the thumbnail id user the term_id
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
// print the IMG HTML
echo '<img src="'.$image.'" alt="" width="30" height="30" />';
}?>
I am running a woocommerce shop and I have products which are in multiple categories. I would like to show the category images on the single product pages. And also on the product overview page.
I only found how I can show the category image on the product category page:
<?php
if (is_product_category()){
global $wp_query;
// get the query object
$cat = $wp_query->get_queried_object();
// get the thumbnail id user the term_id
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
// print the IMG HTML
echo '<img src="'.$image.'" alt="" width="30" height="30" />';
}?>
Share
Improve this question
asked Feb 22, 2013 at 15:48
Yasp0Yasp0
111 gold badge1 silver badge2 bronze badges
1
- Anyone with some tips? – Yasp0 Commented Apr 5, 2013 at 14:49
3 Answers
Reset to default 1I had the same problem and I came up with this solution, hope it helps.
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}
?>
I think you should try this
if ( is_product_category( array( 'cat-1', 'cat-2' ) ) ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img src="' . $image . '" alt="" />';
}
}
This shows the images of only cat-1 and cat-2 category.
Worked brilliantly!!
<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img src="'.$image.'">';
}
?>
本文标签: categoriesShow category images on single product page and product overview page
版权声明:本文标题:categories - Show category images on single product page and product overview page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741791287a2397671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论