不显示类别页面上的产品名称和数量

时间:2018-05-03 13:52:01

标签: wordpress woocommerce

我有问题。我有用于在类别页面上输出类别名称和产品数量的代码,但在将版本3.2.6更新为3.3.5后,名称不再显示

这是代码

function add_product_count_view() {
global $post;
$terms = get_term_by( $post->ID, 'product_cat');
foreach( $terms as $term ) {
    if(is_tax('product_cat', $term->name)) {
        echo '<h3 class="woo-category-name">'
            . $term->name
            .'<span class="woo-category-count">'
            . '('
            . $term->count
            . ')'
            .'<span/>'
            . '</h3>';
    }
  }
}

还有这样的错误:警告:在第18行的/home/d/devenup/evok.dvnup.com/public_html/wp-content/themes/evok/functions.php中为forach()提供的参数无效< / p>

但是在更新之前和之前

1 个答案:

答案 0 :(得分:0)

产品是wordpress的简单帖子,所以你可以像普通帖子一样使用它。请尝试以下代码并根据需要进行修改。

global $post;

    $terms = get_the_terms( $post->ID, 'product_cat' );
if(is_array($terms)){
    foreach ($terms as $term) {
        $product_cat = $term->name;
        break;
    }
}