【问题标题】:Wordpress categories list, description and post countWordpress 类别列表、描述和帖子计数
【发布时间】:2013-10-15 21:52:46
【问题描述】:

我正在寻找一种方法来列出所有类别及其描述,并在每个类别旁边显示一个数字,表示每个类别中有多少帖子(已发布)。

所以输出会是这样的:

类别名称 1
说明(3)

类别名称 2
说明 (16)

类别名称 3
说明(7)

..etc,其中括号中的数字是该类别中的帖子数。

我一直在使用这个,它很接近,但它只需要提到的帖子数量。

<?php
$categories = get_categories('exclude=1&title_li=');
foreach ($categories as $cat) {
echo "<h2><a href=\"".$cat->category_nicename."\">".$cat->cat_name."</a></h2>
      <p>".$cat->category_description."</p>";
   }
?>

提前致谢。

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    get_categories() 返回类别计数 - 请参阅 http://codex.wordpress.org/Function_Reference/get_categories#Return_values

    ...所以你可以像这样用$cat-&gt;category_count 插入它:

    <?php
    $categories = get_categories('exclude=1&title_li=');
    foreach ($categories as $cat) {
        echo "<h2><a href=\"".$cat->category_nicename."\">".$cat->cat_name."</a></h2>
              <p>".$cat->category_description."(".$cat->category_count.")</p>";
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多