php - WooCommerce 在产品标题中显示产品类别

标签 php wordpress woocommerce taxonomy

我有一个运行 WooCommerce(2.3.8 版)的 Wordpress(4.2.2 版)电子商务网站。

在我的个人产品页面上,我希望将产品的标题设置为还包括我在 WooCommerce 中创建的以及该产品所属的自定义类别。

我找到以下与单个产品的标题相关的文件 (wp-content/themes/mytheme/woocommerce/single-product/title.php) 并按如下方式对其进行编辑以尝试包含该产品的类别也属于标题。

使用下面的代码,我设法显示类别,但问题是我显示的是所有类别,而不仅仅是该产品所属的类别。

请问如何将返回的类别限制为产品所属的类别?

<?php
if ( ! defined( 'ABSPATH' ) ) 
    exit; // Exit if accessed directly
?>

<!-- Original Product Title START -->
<h1 itemprop="name" class="product-title entry-title">
    <?php the_title(); ?>
</h1>
<!-- Original Product Title END -->


<!-- New Product Title START -->
<h1 itemprop="name" class="product-title entry-title">
    <?php
        $taxonomy     = 'product_cat';
        $orderby      = 'name';  
        $show_count   = 0;      // 1 for yes, 0 for no
        $pad_counts   = 0;      // 1 for yes, 0 for no
        $hierarchical = 1;      // 1 for yes, 0 for no  
        $title        = '';  
        $empty        = 0;
        $args = array(
            'taxonomy'     => $taxonomy,
            'orderby'      => $orderby,
            'show_count'   => $show_count,
            'pad_counts'   => $pad_counts,
            'hierarchical' => $hierarchical,
            'title_li'     => $title,
            'hide_empty'   => $empty
        );
    ?>

    <?php 
        $all_categories = get_categories( $args );

        foreach ($all_categories as $cat) 
        {
            if($cat->category_parent == 0) 
            {
                $category_id = $cat->term_id;
    ?>      

    <?php       
        echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; 
    ?>

    <?php
        $args2 = array(
            'taxonomy'     => $taxonomy,
            'child_of'     => 0,
            'parent'       => $category_id,
            'orderby'      => $orderby,
            'show_count'   => $show_count,
            'pad_counts'   => $pad_counts,
            'hierarchical' => $hierarchical,
            'title_li'     => $title,
            'hide_empty'   => $empty
        );

        $sub_cats = get_categories( $args2 );

        if($sub_cats) 
        {
            foreach($sub_cats as $sub_category) 
            {
                echo  '<br/><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
                echo apply_filters( 'woocommerce_subcategory_count_html', ' <span class="cat-count">' . $sub_category->count . '</span>', $category );
            }
        }
    ?>

    <?php 
            }     
        }
    ?>
</h1>
<!-- New Product Title END -->

最佳答案

您可以使用 get_the_terms 轻松获取分配给产品的所有类别

$terms = get_the_terms( get_the_ID(), 'product_cat' );

foreach ($terms as $term) {

    echo '<h1 itemprop="name" class="product-title entry-title">'.$term->name.'</h1>';
}

关于php - WooCommerce 在产品标题中显示产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30592156/

相关文章:

javascript - 即使正确,使用 ajax 验证的 reCAPTCHA 也总是返回错误

wordpress - WooCommerce [product_categories] 短代码 - 缺少显示的子类别

php - 在购物车商品库存问题上禁用 WooCommerce 下订单结帐按钮

php - 复选框搜索 php

php - PDO 未选择数据库

php - 如何从数据库中检索图像并在网页上显示图像

php - 将 WordPress 与 PayPal IPN 集成

php - Wordpress:使用数组手动将类添加到 post_class?

php - Wordpress 多级下拉菜单

php - 在 WooCommerce 中,随着自定义库存数量的减少,将 "out-of-stock"产品变体变灰