WordPress.org

Support

Support » Themes and Templates » Display the number of posts beside category

Display the number of posts beside category

  • abhik03
    Member

    @abhik03

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • vossavant
    Member

    @vossavant

    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:

    <ul>
    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>';
    }
    </ul>

    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.

    vossavant

    mibsolutionsllc
    Member

    @mibsolutionsllc

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display the number of posts beside category’ is closed to new replies.