WooCommerce 在产品标题中显示产品类别 [英] WooCommerce display product categories in product title

查看:43
本文介绍了WooCommerce 在产品标题中显示产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Wordpress (version 4.2.2) eCommerce site running WooCommerce (version 2.3.8).

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

On my individual product page I wish to set the title of the product to also include the custom categories I have created in WooCommerce and that this product belongs to.

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

I find the following file (wp-content/themes/mytheme/woocommerce/single-product/title.php) that relates to the title of the individual product and edit it as below to try and include the categories that this product belongs to in the title as well.

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

With the code below I manage to display the categories, but the problem is that I am displaying ALL categories, and not just the categories that this product belongs to.

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

How do I limit the categories returned to only the categories that the product belongs to please?

<?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 简单地获取分配给产品的所有类别

you can simply get all the categories assign to product by using 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>';
}

这篇关于WooCommerce 在产品标题中显示产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆