I tried using get_the_categories(); to do this as suggested elsewhere, but it either conflicts with something else i have or it’s not returning the correct amount…
In any case i wrote this little piece of PHP will does give the correct total of categories..
<?php
$thecats = wp_list_categories('title_li=&style=none&echo=0'); // Get a list of categories, minus title and HTML list..
$splitcats = explode('<br />',$thecats); // Explode into array where tags are found (end of each category)..
$summ = count($splitcats)-1; // Count the total, minus 1.. Explode seems to add +1..
?>
There are a total of <?php print $summ; // Print the numeric amount ie. 1, 2 etc... ?> categories.
Result::
There are a total of X categories.
Where X is the number… ??
I’m sure this is a poor approach, but as said i got incorrect results when trying to use get_the_categories….
By all means if someone can produce a better way i would advise it, but for now this should work…
There is one flipside though, this does not include empty categories…
wp_list_categories will support quite a few extras like depth=1, which should just return categories that are top level cats…
wp_list_categories('title_li=&style=none&echo=0&depth=1')
Just be sure to leave the bits i’ve put in there are the code will not function as intended, otherwise you’re free to add other bits in…
Again there’s proberly a much more efficient way, but the above was the only way it was giving the correct result…
To run down on what has been done, first i grab the list of cats… specifying the title to be left out, along with the list HTML, ie, LI elements etc…
Which then procudes…
<a href="">Cat 1 </a>
<a href="">Cat 2 </a>
<a href="">Cat 3 </a>
<a href="">Cat 4 </a>
Then explode, which put simply splits the result where a delimeter is found (also removing that delimeter, in this case i used the line break to split them… and place them into an array…
Count will give you the total amount of items in an array…
So simply we then just echo out the result of counting the array….
I seemed to end up with +1 for some reason, so i subtracted one of the end result…