• I’ve searched the site and can’t find an answer to something that is probably very simple so: help! I need a way to get the total number of posts in a category, to be displayed such as “there are X number of posts in this category”. I’m aware of the optioncount parameter in wp_list_cats but that’s not what I want. I just want to be able to display the number of posts for one category. Is there a simple plugin or hack to do this? Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • ^^ Yea I want to be able to do that too-i cant figure out how for the life of me. 🙁 any help would be appreciated!!! thanks everyone.

    Hmm. I thought I had seen a hack/plugin that does this, but can’t come up with it. Anyway, it’s a (ok, relatively) simple bit of code you can place in your page template or my-hacks.php:

    <?php
    if($cat)
    $postsincat = count($wpdb->get_col("SELECT post_id FROM $tablepost2cat WHERE category_id = $cat"));
    ?>

    Then to display it:

    <?php echo $postsincat; ?>

    This is very helpful, but I’ve got a questio expanding on it. When viewing a single post, I’d like to be able to display anywhere on the page something like:

    “You are reading post X of Y total posts in this category”

    The code you’ve written above should help me substitute the total number of posts for the “Y” above (thanks!), but how can I get the “X” to display the number of the current post you’re reading?

    Thanks for any ideas you have.

    Make it a function (to be included within the ‘function.php’ in your theme folder, for example):

    function number_postpercat ($idcat, $post_status='publish') {
    global $wpdb;
    $result=$wpdb->get_col("SELECT c.post_id FROM {$wpdb->post2cat} c INNER JOIN {$wpdb->posts} p ON c.post_id = p.id WHERE c.category_id = $idcat AND p.post_status='$post_status'");
    $number=count($result);
    echo $number;
    }

    Then trigger it including the following in the appropiate place of your template:
    <?php number_postpercat (4); ?>
    …where ‘4’ is the required category id.

    Have a good day,
    hip

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Number of posts’ is closed to new replies.