【问题标题】:Show the amount of posts in a tag in a specific category has显示特定类别标签中的帖子数量
【发布时间】:2025-12-04 23:25:01
【问题描述】:

我正在 Wordpress 中创建一个页面模板,该模板显示特定类别中的多个标签。我有这个工作,但现在我想显示每个标签中的帖子数量,就像我有一个名为 apples 的标签有 5 个帖子,它看起来像这样:

Apples(5)

现在它只显示Apples

这是我要修改的代码:


    <?php
      if (is_category()){
           $cat = get_query_var('cat');
           $yourcat = get_category ($cat);
       }
       $tag_IDs = array();
           query_posts('category_name=health');
                   if (have_posts()) : while (have_posts()) : the_post();
                       $posttags = get_the_tags();
                           if ($posttags):
                           foreach($posttags as $tag) {
                               if (!in_array($tag->term_id , $tag_IDs)):
                                   $tag_IDs[] = $tag->term_id;
                                   $tag_names[$tag->term_id] = $tag->name;
                                   endif;
                       }
                       endif;
                       endwhile; endif;
                   wp_reset_query();
       
               echo "<ul>";
               foreach($tag_IDs as $tag_ID){
               echo '<a href="'.get_tag_link($tag_ID).'">'.$tag_names[$tag_ID].'</a>';
               }
           echo "</ul>";
       ?>                                  

【问题讨论】:

    标签: wordpress tags categories


    【解决方案1】:

    我认为您正在寻找以下内容:

    在当前查询的循环之外:

    <?php echo $wp_query->get_queried_object()->count; ?>
    

    每个术语的循环内部:

    <?php echo $tag->count; ?>
    

    问候。

    【讨论】: