在Woocommerce单一产品页面中获取产品类别名称和描述 [英] Get the product category name and description in Woocommerce Single Product page

查看:62
本文介绍了在Woocommerce单一产品页面中获取产品类别名称和描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用WooCommerce Codex,但似乎无法显示数据.我只想显示产品类别和说明,以便在我自己的自定义布局的单个产品页面上显示,就像这样.

I've been using the WooCommerce Codex, but I can't seem to get the data to display. I simply want to display the product category and descriptions to display on single product pages for my own custom layout like so.

<?php global $product; echo $product->get_attributes; ?>

<?php global $product; echo $product->get_short_description; ?>

推荐答案

由于产品可以具有许多产品类别,因此需要使用foreach循环. $ term 变量是 WP_Term 对象……

As you can have many product categories for a product, you will need to use a foreach loop. The $term variable is the WP_Term object…

<?php 
    foreach( wp_get_post_terms( get_the_id(), 'product_cat' ) as $term ){
        if( $term ){
            echo $term->name . '<br>'; // product category name
            if ($term->description)
                echo $term->description . '<br>'; // Product category description
        }
    }
?>

经测试可正常工作

这篇关于在Woocommerce单一产品页面中获取产品类别名称和描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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