Display the number of posts beside category (3 posts)

  1. abhik03
    Posted 7 years ago #

    How can I display the number of posts just beside the category name in Sidebar?
    I guess theres some code need to be added.

  2. vossavant
    Posted 7 years ago #

    If you are content with showing it like this:

    Category (x)
    Category (y)

    Where x and y are the number of posts in the category, then just be sure to include the "show_count" parameter in the wp_list_categories function:

    <?php wp_list_categories('show_count=1'); ?>

    As for displaying the posts in a fancier way, paste the following snippet of code where you want to show your categories:

    foreach (get_categories(array('hide_empty'=>false)) as $category)
    echo '<li><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">' . $category->cat_name . '</a> (' . $category->count . ' posts)</li>';

    This will output:

    Category Name (X posts)

    You can wrap the (X posts) piece in a <span> tag or other HTML tag if you want to style it separately. In my case, I was looking to position the category name to the left and the number of posts to the right.

    Special thanks to John Herren for this one.


  3. mibsolutionsllc
    Posted 6 years ago #

    How would I do the same idea, but for tags?

Topic Closed

This topic has been closed to new replies.

About this Topic