• Dear all,

    I am trying to display the total number of categories used on the blog within a text. I found this forum article which does show the total number of categories used.

    It does however also include categories that exist but have not been posted to. I currently have 9 categories et up in my admin interface but only use 7 so far. The number displayed should be 7 and not 9.

    Does anybody know how to do this. Any help would be appreciated.

    Thanks a lot,

    Martin

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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…

    Strange forum seemed to strip out the BR part initially….

    Perhaps i found a bug in the BBPress backtick shortcode functionality… any case it’s showing above now, and that’s what matters… 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to show the total number of categories (only those with posts)’ is closed to new replies.