WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to show the number of posts from one category on category archive page (6 posts)

  1. mfuchs
    Member
    Posted 5 years ago #

    Hey there together,

    I ran into yet another problem that I can't seem to figure out even after searching the forum and consulting Google...

    On my category archive page I want to display the number of posts from the category the user is just looking at. And I don't want to install yet another plugin to do that, there must be some simple one lince code that I could just add to the template file.

    Any help would be highly appreciated!

    Thanks a lot,
    Martin

  2. MichaelH
    Member
    Posted 5 years ago #

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('include='.$cat);
    if ($categories) {
      foreach($categories as $category) {
        echo 'Posts in this category = ' . $category->count;
      }
    }
    ?>
  3. mfuchs
    Member
    Posted 5 years ago #

    Thanks a lot Michael. That worked. Would you be able to let me know the same kind of code to use to display the total number of posts within a monthly archive?

  4. MichaelH
    Member
    Posted 5 years ago #

    display the total number of posts within a monthly archive

    <?php
    if ( is_month() ) {
    $month = get_query_var('monthnum');
    $year = get_query_var('year');
    $countposts=get_posts("year=$year&monthnum=$month");
    echo 'the count of posts in ' . $month .'/'.$year . ' is '  . count($countposts);
    }
    ?>
  5. 67thavenue
    Member
    Posted 5 years ago #

    What's the code to display the number of post for just one particular category? For example: Comic.

    Thanks!

  6. AlDavies
    Member
    Posted 4 years ago #

    Hi 67thavenue,

    Try this code where 123 is the ID of the category. This will single out just that category.

    <?php
    $cat = get_query_var('cat');
    $categories=get_categories('include=123'.$cat);
    if ($categories) {
      foreach($categories as $category) {
        echo '' . $category->count;
      }
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic