I need to calculate some dynamic CSS based on how many top level categories exist. (NOT how many posts are in a category, or how many categories a post belongs to… Nothing about posts at all, just how many top level categories there are)

I am new to PHP, but I couldn’t find any WP functions that would return the number I’m looking for.

Thanks in advance for any help.

2 Answers
2

As the Codex says, this is the code block to query for only the Top Label categories — the parents. With this, I used the PHP function count().

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

$categories = get_categories( $args );

echo count( $categories );    
?>

Leave a Reply

Your email address will not be published. Required fields are marked *