对于我的 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');
}
...但是我如何获得 WooCommerce 猫 ID?
最佳答案
一个座便器产品可能不属于任何一个、一个或多个座便器类别。假设您只想获得一个 WC 类别 ID。
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
请查看 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>' ); ?>
关于php - WooCommerce - 获取产品页面的类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15303031/