【问题标题】:How to display Woocommerce category description如何显示 Woocommerce 类别描述
【发布时间】:2013-10-08 13:58:21
【问题描述】:

我有常规的 wordpress 代码来显示类别描述:

<?php echo category_description( $category_id ); ?>

但是如何显示 Woocommerce 类别描述? @@ 在我添加了一条评论建议后:

                    <?php 
    if ( have_posts() ) {
        while ( have_posts() ) {
            the_post(); 
global $post, $product; $categ = $product->get_categories(); $term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' ); echo $term->description; 
        } // end while
    } // end if
?>

还是不行。

【问题讨论】:

  • Woo 文档显示分类法是“product_cat”...这行得通吗? : get_categories(); $term = get_term_by ('name', strip_tags($categ), 'product_cat');回声$术语->描述; ?>
  • @Matt.C nop。我也在谷歌上找到了它,但它不起作用。无论如何谢谢!
  • 您能否再发布一些代码,您是否在循环中使用它?

标签: php wordpress woocommerce


【解决方案1】:
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);

$count = count($terms); 
if ($count > 0) {

   foreach ($terms as $term) {
        echo $term->description;
   }
}

编辑最后一个答案:

<?php
global $post;
$args  = array(
    'taxonomy' => 'product_cat'
);
$terms = wp_get_post_terms($post->ID, 'product_cat', $args);

$count = count($terms);
if ($count > 0) {

    foreach ($terms as $term) {
        echo '<div style="direction:rtl;">';
        echo $term->description;
        echo '</div>';

    }
}

【讨论】:

  • 有效!!谢谢你。现在只是问题 - 它显示任何类别中的所有类别描述。我怎样才能只显示当前的类别描述?
  • codex.wordpress.org/Function_Reference/get_terms 在这里检查您有要传递的参数来优化您的结果
  • 要问:你检索条款的行不能缩在一行中,这样:$terms = get_the_terms($post-&gt;ID, 'product_cat');?你仍然会得到一个 WP_Terms 对象数组,所以 foreach 仍然会以同样的方式工作:)
【解决方案2】:

您可以显示产品类别描述 -

使用此代码 -

<?php global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description; ?>

【讨论】:

    【解决方案3】:

    the_archive_description() 为我的目的工作,而其他(更复杂的)解决方案却不能。

    如果需要,可以添加可选的前后字符串参数。

    【讨论】:

      【解决方案4】:

      出于某种原因,主要答案为我显示了不止一个描述。

      下面的答案为任何有同样问题的人解决了这个问题:

      https://stackoverflow.com/a/19266706/2703913

      【讨论】:

      • 将答案从那里复制到这里会很有用,可以节省人们点击并重新阅读问题
      • 我不想把它当作我自己的。我也可以什么都不做,所以如果它帮助你,只是说声谢谢而不是不必要的呻吟。
      猜你喜欢
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      相关资源
      最近更新 更多