• Hi all.

    I want to show the number of posts in a category. I just want the number and not a list of the categories too. So if I have 5 post in a category called “web”, I want to display just the “5” and not “web (5)”. Are you with me? So I guess that excludes list_cats .. Correct me if I’m wrong.. Would really apreciate any help..

Viewing 4 replies - 1 through 4 (of 4 total)
  • I think you’ll have to write a custom MySQL query to achieve that. I’ve never heard of such a thing.

    Hye, I’ve got the same problem, referred to one of the docs but it is apparently deprecated. Now what?

    Thanks!

    If there’s still someone interested in a way, I needed something similar and came up with a little “hack” abusing query_posts()…

    <?php $myquery = $wp_query;
    query_posts('cat=a&showposts=999');
    $numberofposts_a = $wp_query->post_count;
    // [...]
    query_posts('cat=b&showposts=999');
    $numberofposts_b = $wp_query->post_count;
    // [...] as often as you like for each category a, b, c, d, etc.
    $wp_query = $myquery; ?>

    This way, the plain number of posts is being assigned to the ‘$numberofposts-x’ variables. Of course, you have to set the query variable ‘showposts’ in your query to a higher value than the number of existing posts in each category.

    Hi & thanks for pointing in the right direction, however, the solution above messed up the number of posts being displayed in my single post view. I resorted to the function used in the post-count plugin and added a category ‘filter’:

    <?php 
    
    function mdv_post_count() {
    global $wpdb;
    echo $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_category = 'your-category-name' AND post_date_gmt < '" . gmdate("Y-m-d H:i:s",time()) . "'");
    }
    ?>

    Call the result in your template using <?php mdv_post_count(); ?>

    (I didn’t use the plugin but pasted the function into a functions.php file in my theme folder. You could duplicate and rename the function for different categories)

    Anyway, figured this might be helpful to someone. For a bit more completeness there’s also the msg-count plugin which you could modify the same way.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Show number of posts in category, just the number and not the category name’ is closed to new replies.