admin管理员组文章数量:1428258
I have tried to display woocommerce categories thumbnails, but nothing works, below is the code to display the categories list and it works awesomely, just one thing left the thumbnails.
$product_cats = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => 0,
'fields' => 'names' // Term names
]);
foreach ( $product_cats as $key => $parent_term_name ) {
printf( '<button class="tablinks %s" onclick="%s">%s</button>',
$key === 0 ? esc_attr( 'active' ) : '',
"myFunction(event,'{$parent_term_name})",
$parent_term_name
);
}
I have tried to add this part so that I could display the image, but nothing works beside from the placeholder image.
$category_thumbnail = get_woocommerce_term_meta($parent_term_name->term_id, 'thumbnail_id', true);
if ( $category_thumbnail ) {
$image = wp_get_attachment_url($category_thumbnail);
}
else {
$image = wc_placeholder_img_src();
}
I have tried to display woocommerce categories thumbnails, but nothing works, below is the code to display the categories list and it works awesomely, just one thing left the thumbnails.
$product_cats = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => 0,
'fields' => 'names' // Term names
]);
foreach ( $product_cats as $key => $parent_term_name ) {
printf( '<button class="tablinks %s" onclick="%s">%s</button>',
$key === 0 ? esc_attr( 'active' ) : '',
"myFunction(event,'{$parent_term_name})",
$parent_term_name
);
}
I have tried to add this part so that I could display the image, but nothing works beside from the placeholder image.
$category_thumbnail = get_woocommerce_term_meta($parent_term_name->term_id, 'thumbnail_id', true);
if ( $category_thumbnail ) {
$image = wp_get_attachment_url($category_thumbnail);
}
else {
$image = wc_placeholder_img_src();
}
Share
Improve this question
edited May 6, 2019 at 3:50
LoicTheAztec
3,39117 silver badges24 bronze badges
asked May 6, 2019 at 2:28
MitosMitos
557 bronze badges
1 Answer
Reset to default 2To include the product category thumbnail (125 × 125 px) in your product category items (buttons), use the following:
$product_cats = get_terms([
'taxonomy' => 'product_cat',
'hide_empty' => true,
'parent' => 0,
]);
foreach ( $product_cats as $key => $parent_term ) {
$thumb_id = get_woocommerce_term_meta( $parent_term->term_id, 'thumbnail_id', true );
$size = 'thumbnail';
$image = wp_get_attachment_image_src($thumb_id, $size);
printf( '<button class="tablinks %s" onclick="%s"><img src="%s" width="150" height="150" />%s</button>',
$key === 0 ? esc_attr( 'active' ) : '',
"myFunction(event,'{$parent_term->name}')",
$thumb_id ? $image[0] : wc_placeholder_img_src($size),
$parent_term->name
);
}
本文标签: phpHow to display product cropped thumbnail (150x150) for WooCommerce product categories
版权声明:本文标题:php - How to display product cropped thumbnail (150x150) for WooCommerce product categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745521769a2661657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论