Matthew
Matthew

Reputation: 81

Count the TOTAL number of categories in Wordpress?

With or without posts within. I need to display the total number of categories in my Wordpress theme.

Upvotes: 2

Views: 7639

Answers (4)

Mahdi Afzal
Mahdi Afzal

Reputation: 827

Retrieve list of category objects 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>

Upvotes: 0

RDS
RDS

Reputation: 39

Easy way to count category is: 1) firstly fetch all category from wordpress 2) count them using simple php funciton complete code will be like:

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

$categories = get_categories( $args );

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

i used this code always :)

Upvotes: 3

Stubbies
Stubbies

Reputation: 3124

A more straight forward way (perhaps faster?)

global $wpdb;

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

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

Upvotes: 0

Matthew
Matthew

Reputation: 81

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

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

Upvotes: 1

Related Questions