How to: Display a text/code only if more than X posts are published

On Cats Who Code, I shown you how to integrate a pagination on your theme. That’s a nice trick, but if you styled it with css and have less than 11 posts published, it will only display a blank container. Let’s fix it.

To achieve this recipe, we're going to use a cool new function, introduced in WordPress 2.7, and called wp_count_posts().

Paste the following code anywhere on your template:

<?php
$count_posts = wp_count_posts();
if ($count_posts->publish > 10) {
    //Your code to be displayed only if more than ten posts have been published
}
?>

That's all. Note that you'll need to replace 10 by the number of posts which are displayed at the same time on your homepage.

6 Responses

Feb 01 2009 00:41

Well, simple but really useful. Thanks! ;)

Feb 01 2009 18:52

Nice, I can imagine this also being useful for starting-up blogs- for example wrapped around a set of post stats that only appear when there have been a decent amount of posts to warrant showing the information.

Feb 03 2009 03:45

Let’s me test it ;) thanks

Feb 03 2009 18:19

I suggest to take a look even at http://www.themeshaper.com/thematic/ to achieve a similar result. do you know the power of wordpress frameworks and child themes???

Feb 22 2009 21:32

First create query(test).

SELECT *
FROM post
WHERE POST LIKE
ORDER BY ID

then:

#TEST.POST#

Try to experiment.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required