计算Wordpress中的总类别数量?

时间:2016-12-28 23:59:41

标签: wordpress count categories

有或没有帖子。我需要在Wordpress主题中显示类别总数。

4 个答案:

答案 0 :(得分:1)

计算类别的简便方法是: 1)首先从wordpress中获取所有类别 2)使用简单的php函数计算它们  完整的代码将如下:

<?php $args = array( 'parent' => 0, 'hide_empty' => 0 );

$categories = get_categories( $args );

echo "Total categories : ".count( $categories );    ?>

我总是使用这段代码:)

答案 1 :(得分:0)

<?php    
$args = array(
'get' => 'all',
'hide_empty' => 0
);

$categories = get_categories( $args );
echo count( $categories );    
?>

答案 2 :(得分:0)

更直接的方式(也许更快?)

global $wpdb;

$categories_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->terms" );

echo "<p>Categories count is {$categories_count}</p>";

答案 3 :(得分:0)

检索类别对象get_categories()的列表:

<ul class="list-group">
  <?php
  $categories = get_categories();
  $i = 0;
  foreach ($categories as $category) {
    $i++;
    echo '<li class="list-group-item"><a href="' . get_category_link($category->term_id) . '"><span class="text-muted float-left">' . $category->category_count . '</span>' . $category->name . '</a></li>';
  }
  ?>
</ul>