php - WooCommerce - 获取产品页面的类别

标签 php wordpress woocommerce

对于我的 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/

相关文章:

wordpress - Wordpress/BuddyPress 注册中的自定义电子邮件验证

css - h1 :after badge not lining up properly

javascript - WooCommerce - 使用 add_to_cart 后显示更新的迷你购物车小部件以及更新的购物车内容/总计

php - 使用算法更新表条目

php - 如何组合 sql 表中的 2 列来搜索全名

wordpress - 更改悬停菜单颜色

wordpress - woocommerce-以编程方式更新购物车项目数量

php - 在 WooCommerce 结帐表单中显示自定义表格值

c# - PHP 和 C# 兼容 Rijndael 托管 CBC 模式,256 位加密/解密

php - 用户注册 php