Forums

[resolved] number of posts per category (5 posts)

  1. Misterpayne112
    Member
    Posted 2 years ago #

    i want to display the number of posts in each category, next to the link for that category in my sidebar. i am using...

    <?php list_cats(FALSE, ' ', 'name'); ?>

    that displays all categories in a nice neat list, but i want the number of posts in each category right next to the link. how do i do that?

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    Maybe try something like..

    <?php
    $args = array('pad_counts' => true, 'get' => 'all');
    $cats = get_terms('category', $args);
    ?>
    <?php foreach( $cats as $category ) : ?>
    	<a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->name; ?></a>
    	<?php echo $category->count; ?> posts<br />
    	<?php unset( $category ); ?>
    <?php endforeach; unset( $cats ); ?>

    Function information:
    http://codex.wordpress.org/Function_Reference/get_terms

  3. MichaelH
    Volunteer
    Posted 2 years ago #

    Remember list_cats is deprecated in favor of the template tag, wp_list_categories(), which has a show_count=1 argument.

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    ... get_terms is faster though ... :p ... ;)

  5. Misterpayne112
    Member
    Posted 2 years ago #

    thanks t31os_ it worked like a charm! Thanks everyone else too!

Topic Closed

This topic has been closed to new replies.

About this Topic