Support » Fixing WordPress » How to show the number of posts from one category on category archive page

  • Resolved mfuchs

    (@mfuchs)


    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • MichaelH

    (@michaelh)

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

    (@mfuchs)

    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?

    MichaelH

    (@michaelh)

    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);
    }
    ?>

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

    Thanks!

    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;
      }
    }
    ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to show the number of posts from one category on category archive page’ is closed to new replies.