• Resolved veevee

    (@veevee)


    I have links built into my site that link to the most recent post in a category using get_posts, and I’d also like to display the number of posts in that category. so the link would look something like:

    <a href="<?php the_permalink() ?>" rel="bookmark">Drawings (<?php show_count category=4?> images in post)</a>

    Except where I just made used an imaginary wordpress tag, there would be one that works for real. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Here’s your imaginary WordPress tag:

    function the_category_count( $cat_id ){
    	$o = wp_cache_get( $cat_id, 'category' );
    	print $o->count;
    }

    And here’s how to use it:

    <a href="<?php get_permalink('4'); ?>">Drawings <?php the_category_count(8); ?></a>

    Thread Starter veevee

    (@veevee)

    Wow. Just like actionscript. That works like a dream. Really I’ve never had an easier time with php. So this is what I ended up with:

    <?php
     global $post;
     $myposts = get_posts('numberposts=1&offset=0&category=4');
     foreach($myposts as $post) :
     setup_postdata($post);
    
     function the_category_count( $cat_id ){
    	$o = wp_cache_get( $cat_id, 'category' );
    	print $o->count;
    }
    ?>
    	<li>
    	<a href="<?php the_permalink() ?>" rel="bookmark">Drawings(<?php the_category_count(4); ?>)		</a>
     	</li>	
    
     <?php endforeach; ?>

    So I already had the global $post part help me link to the most recent post in a category. I just added the count function to that and then called the function below that. The number in the paren refers to the category id. And even though i need to repeat the global $post code for every single link i make, I only needed to use the count function once. Seamless mfields, really really good.

    Thanks! Glad it worked for you.

    One minor bit of criticism though, you placed the function definition inside of a foreach loop. If you hadn’t defined numberposts as “1” in get_posts(), your script would be generating an error for “function already defined”.

    The best thing that you could do to avoid this is to put the definition of the_category_count() in your themes functions.php file. If your theme does not have one, you can just make one and all functions defined within will be available in WordPress.

    I am sorry but I’m not clear on how to implement this. I’m a PHP newbie…

    I have added the function to the functions.php file and it works great when I use the code exactly the way you have it above. But I don’t want to have it in a loop. Also, the “get_posts” line is referring to a specific category. Shouldn’t we just pass the category ID in the basic call to the function?

    I want to get a specific category count number only. Why doesn’t <?php the_category_count(4); ?> work on its own? Is there anything from the first part of the code above that can be added to the function to make it work?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How do I get show_count to return just a number?’ is closed to new replies.