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?
…但是我怎么才能得到猫的身份呢?
3 个解决方案
#1
56
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插件的“模板/单产品/”文件夹中的php文件。
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
#2
4
I literally striped out this line of code from content-single-popup.php located in woocommerce folder in my theme directory.
我把这行代码从内容单弹出框中删除。位于我的主题目录的woocommerce文件夹中的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,这就是我的解决方案。
#3
1
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
由于盒子。我使用的是神秘主题,我需要在我的搜索结果页面中显示产品类别名称。我将这个函数添加到子主题函数。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
}
}
?>
#1
56
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插件的“模板/单产品/”文件夹中的php文件。
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
#2
4
I literally striped out this line of code from content-single-popup.php located in woocommerce folder in my theme directory.
我把这行代码从内容单弹出框中删除。位于我的主题目录的woocommerce文件夹中的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,这就是我的解决方案。
#3
1
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
由于盒子。我使用的是神秘主题,我需要在我的搜索结果页面中显示产品类别名称。我将这个函数添加到子主题函数。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
}
}
?>