在没有缩略图的 Woocommerce Wordpress 插件中显示产品类别列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15880294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show Product category list in Woocommerce Wordpress Plugin without thumbnails
提问by Umair Khan Jadoon
I am using the following short code to list the product categories in woocommerce Wordpress plugin
我正在使用以下短代码列出 woocommerce Wordpress 插件中的产品类别
<?php echo do_shortcode('[product_categories number=""]'); ?>
The problem is the category thumbnail that I need to get rid of. I want to hide it and doing it with CSS seems to be a big hassle. Is there anyway I can list the categories without the thumbnails appearing?
问题是我需要删除的类别缩略图。我想隐藏它,用 CSS 做它似乎是一个很大的麻烦。无论如何,我可以在不显示缩略图的情况下列出类别吗?
回答by danyo
You could use the following:
您可以使用以下内容:
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);
if (count($terms) > 0) {
echo '<p class="my_term-archive">';
foreach ($terms as $term) {
echo '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
}
echo '</p>';
}
回答by Tomislav Svecak
Heres what I used for making a list of all categories by letter excluding empty letters and with category featured images!
这是我用来按字母列出所有类别的列表,不包括空字母和类别特色图像!
All you need to do is style it the way you want ;)
你需要做的就是按照你想要的方式设计它;)
<?php
/* Define which category // to list child categories array('parent'=>42) 42 is category ID */
$designers = get_terms('product_cat', array('parent'=>42));
/* If categ not empty for each designer take first letter */
if ( !empty( $designers ) && !is_wp_error( $designers ) ){
$term_list = [];
foreach ( $designers as $designer ){
$letter = strtoupper($designer->name[0]);
$designer_list[$letter][] = $designer;
}
unset($designer);
foreach ( $designer_list as $key=>$value ) {
/* Wrap around each designer */
echo '<div><span>' . $key . '</span><ul>';
foreach ( $value as $designer ) {
// Set thumbnail
$thumbnail_id = get_woocommerce_term_meta( $designer->term_id, 'thumbnail_id', true );
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );
/* List designers */
echo '<li><a href="' . get_term_link( $designer ) . '" title="' . sprintf(__('Products by %s', 'my_localization_domain'), $designer->name) . '">' . $designer->name . '<img src=" ' .$image.' "" alt="" /></a></li>';
}
/* end Wrap around designer */
echo '</ul></div>';
}?>
}