php WooCommerce - 获取产品页面的类别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15303031/
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
WooCommerce - get category for product page
提问by ban-geoengineering
For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Here's the function I'm creating for this...
对于我的 WC 产品页面,我需要向 body 标签添加一个类,以便我可以执行一些自定义样式。这是我为此创建的功能...
function my_add_woo_cat_class($classes) {
$wooCatIdForThisProduct = "?????"; //help!
// add 'class-name' to the $classes array
$classes[] = 'my-woo-cat-id-' . $wooCatIdForThisProduct;
// return the $classes array
return $classes;
}
//If we're showing a WC product page
if (is_product()) {
// Add specific CSS class by filter
add_filter('body_class','my_add_woo_cat_class');
}
...but how do I get the WooCommerce cat ID?
...但是我如何获得 WooCommerce 猫 ID?
回答by Box
A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.
WC 产品可能不属于一个、一个或多个 WC 类别。假设您只想获得一个 WC 类别 ID。
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
Please look into the meta.php file in the "templates/single-product/" folder of the WooCommerce plugin.
请查看 WooCommerce 插件的“templates/single-product/”文件夹中的 meta.php 文件。
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
回答by Jaydip Nimavat
$product->get_categories()is deprecated since version 3.0! Use wc_get_product_category_listinstead.
$product->get_categories()自 3.0 版起已弃用!使用wc_get_product_category_list来代替。
https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html
https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html
回答by Alreadytakenindeed
I literally striped out this line of code from content-single-popup.php located in woocommerce folder in my theme directory.
我从位于我的主题目录的 woocommerce 文件夹中的 content-single-popup.php 中删除了这行代码。
global $product;
echo $product->get_categories( ', ', ' ' . _n( ' ', ' ', $cat_count, 'woocommerce' ) . ' ', ' ' );
Since my theme that I am working on has integrated woocommerce in it, this was my solution.
由于我正在研究的主题已将 woocommerce 集成到其中,因此这是我的解决方案。
回答by ZAA.CC
Thanks Box. I'm using MyStile Theme and I needed to display the product category name in my search result page. I added this function to my child theme functions.php
谢谢盒子。我正在使用 MyStile 主题,我需要在我的搜索结果页面中显示产品类别名称。我将此功能添加到我的子主题functions.php
Hope it helps others.
希望它可以帮助其他人。
/* Post Meta */
if (!function_exists( 'woo_post_meta')) {
function woo_post_meta( ) {
global $woo_options;
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
break;
}
?>
<aside class="post-meta">
<ul>
<li class="post-category">
<?php the_category( ', ', $post->ID) ?>
<?php echo $product_cat; ?>
</li>
<?php the_tags( '<li class="tags">', ', ', '</li>' ); ?>
<?php if ( isset( $woo_options['woo_post_content'] ) && $woo_options['woo_post_content'] == 'excerpt' ) { ?>
<li class="comments"><?php comments_popup_link( __( 'Leave a comment', 'woothemes' ), __( '1 Comment', 'woothemes' ), __( '% Comments', 'woothemes' ) ); ?></li>
<?php } ?>
<?php edit_post_link( __( 'Edit', 'woothemes' ), '<li class="edit">', '</li>' ); ?>
</ul>
</aside>
<?php
}
}
?>

