WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Category Post Count not displayed (4 posts)

  1. retrobeatcom
    Member
    Posted 1 year ago #

    Hello,
    i have a the fowling code into my sidebar:

    <?php wp_list_categories('orderby=name&show_count=1&title_li='); ?>

    The above code displays all my categories and sub-categories including the number of posts in every category.

    I tried to include only one of my categories, so i change the above code to:

    <?php wp_list_categories('orderby=name&show_count=1&title_li=&include=22'); ?>

    The above code gives no results, it returns "NO CATEGORIES".
    In category 22 there are no posts but +20 sub-categories.

    If i change it to:

    <?php wp_list_categories('orderby=name&show_count=1&title_li=&child_of=22'); ?>

    It returns all of the sub-categories but that's not what i need.

    I need only the main category with ID 22 to be displayed with the number of all posts inside sub-cateegories.

    Any kind of help will be well appreciated.
    Regards

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this:

    <?php
    $categories = get_categories('child_of=22');
    $count = 0;
    foreach($categories as $cat) {
    	$count += $cat->count;
    }
    ?>
    <a href="<?php echo esc_url( get_category_link( 22 ) ); ?>"><?php echo get_cat_name( 22 )  ?> (<?php echo $count; ?>)</a>
  3. retrobeatcom
    Member
    Posted 1 year ago #

    Thanks so much!
    That worked just how i needed to :)
    Thank You again!

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you've got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic