WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is what I'm trying to do:

if number of posts is greater than 20 -> display link to another page (which shows all posts) if number of posts is less than 20 -> don't display the link

So far I've been able to return the number of posts using

$count_posts = wp_count_posts('inventory');
$published_posts = $count_posts->publish;

but I don't know where to go from here, any suggestions?

share|improve this question
up vote 7 down vote accepted

If I remember right count of posts in category is stored persistently in category object. So use get_category() or variation of it and fetch the number out of object.

Example code (not tested):

$category = get_category($id);
$count = $category->category_count;

if( $count > $something ) {

    // stuff
} 
share|improve this answer
    
Thanks Rarst, how would I use this to create the conditional? – maikunari Jun 24 '11 at 19:45
    
@maikunari added example snippet – Rarst Jun 24 '11 at 19:52
    
Awesome thank you, will give it a try. – maikunari Jun 24 '11 at 20:20

You can access that from the object itself:

foreach ( get_the_terms( get_the_ID(), 'taxonomy' ) as $term )
{
    printf( '%s (%s)', $term->name, $term->count );
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.