Forums

[resolved] Number of posts in a category (5 posts)

  1. pioneerka
    Member
    Posted 2 years ago #

    Hello,

    Is there any way to display number of published posts in a specific category?

    This code unfortunately displays number of all published posts, in all categories:

    <?php
    $published_posts = wp_count_posts()->publish;
    ?>

    Is there a solution?

    Thanks in advance .)

  2. dcannon1
    Member
    Posted 2 years ago #

    You could always do something like this:

    $the_test_cat = get_category('5');
    $test_count =  $the_test_cat->category_count;
  3. pioneerka
    Member
    Posted 2 years ago #

    dcannon1, thank you! .)

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    Or with the template tag, wp_list_categories():

    List all categories with post count
    wp_list_categories('show_count=1');

    List category 5 with post count
    wp_list_categories('include=5&show_count=1');

    Or use the Category Widget with the Show Count checkbox checked.

  5. pioneerka
    Member
    Posted 2 years ago #

    MichaelH, thank you, but i don't need the title of a category to be displayed, just the number of posts, without any link.

Topic Closed

This topic has been closed to new replies.

About this Topic