• Hi, I’ve been looking everywhere trying to figure out how to display the number of posts per category for WordPress 2.5. I understand there is a parameter for wp_list_categories, but I’d like to avoid this as I do not want to display the category names dynamically. Ideally, I would like to drop something like this in my header –

    <ul id="nav">
    <li><a href="#">Static Category</a> (<?php number_postpercat (4); ?>)</li>
    <li><a href="#">Static Category</a> (<?php number_postpercat (5); ?>)</li>
    </ul>

    number_postpercat would be a function that grabs the total posts from whatever cat id i define. 4 and 5 are example cat id’s.

    I found a post from 3 years ago on the forums located here:
    https://www.ads-software.com/support/topic/12805

    The person was looking for something similar. I tried both methods but neither worked.

    Hopefully you can help! Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You’re not going to be able to have your cake and eat it too in this instance. You can’t manually insert your category and expect WordPress to magically figure out how many posts are in that category.

    The link to that thread you posted is pretty much your only hope.

    Try

    <ul id="nav">
    <?php
    wp_list_categories('show_count=1'); ?>
    </ul>

    You should be able to mess around with it to appear as u want

    Thread Starter superslick

    (@superslick)

    Hi, again, I would like to avoid wp_list_categories.

    This was posted in another forum and it’s exactly what i’m looking for, but doesn’t seem to work on 2.5

    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

    superslick, try this in functions.php:

    function number_postpercat($idcat) {
    	global $wpdb;
    	$query = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat";
     	$num = $wpdb->get_col($query);
    	echo $num[0];
    }

    And then call it with <?php number_postpercat (4); ?> in your template…

    Works for me on WP 2.6

    @jotaklot That’s an sql injection attack waiting to happen! You need to escape the parameter in the query.

    I had a friend pose this question to me, so I wrote a post on my blog with a solution using already available WordPress functions. Hope this helps someone.

    John,

    I posted a thank you on your blog for your insight on this. I was looking for more control over the output of the number of posts for each category. Instead of the default output:

    Uncategorized (2)

    I wanted something like this:

    Uncategorized // 2 posts

    After looking at your code I came up with this:

    foreach (get_categories(array('hide_empty'=>false)) as $category)
    {
    	echo '<li><a href="' . get_bloginfo('wpurl') .
    	'/category/' . $category->category_nicename . '/">' .
    	$category->cat_name . '</a> // ' . $category->count .
    	'posts</li>';
    }

    Simply drop this code in between some <ul> tags where you want your category list to appear and you’re set. It’s also possible to wrap the “number of posts” piece in a <span> or similar tag to style it differently.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘display number of posts per category’ is closed to new replies.