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

January 31, 2009 at 8:35 am

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.

Tags: