Wordpress: Add Content Based on Post Count
A couple months ago I redesigned one of my sites to use Wordpress as a CMS. It is a site that displays small video clips with a few lines of text underneath. Everything went well with the transformation and I was happy with the way it turned out, but there was one minor problem.
The pages on the site are setup to show 15 posts per page. If there aren’t 15 posts on the page, the layout gets broken. Pages 1 & 2 are fine, but page 3 doesn’t have 15 posts yet. You can see from the screenshot below what I’m talking about.
Somehow I needed to add blank posts at the end to fill up the rest of the page. Yesterday, while looking through a Wordpress tip site, I found something that made me think it would be possible. Display a text/code only if more than X posts are published
<?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 code would have worked fine, but I needed it to display a dynamic number of blank posts based on the current post count. I also needed it to only display those blank posts on page 3. It was a good starting point though.
Let me start off by saying that I am NOT a php programmer, so this may be a little messy. I knew what I wanted it to do, but I had to Google everything to see how it was done in php. After piecing everything together, I finally came up with this:
1 2 3 4 5 6 7 | <?php $page_url=$_SERVER['REQUEST_URI']; $page_number= 'page/3'; $published_posts = wp_count_posts()->publish; while ($published_posts < 45 && strstr($page_url, $page_number) ) { ?> <div class="videoitem">...</div> <?php $published_posts = $published_posts + 1; if($published_posts == 45) break; } ?> |
Line 1 – start php
Line 2 – reads the URL of the website
Line 3 – declares $page_number as “page/3″.
Line 4 – uses built in function of Wordpress to declare $published_posts
Line 5 – says that while the number of posts is less than 45 and the URL contains ”page/3″, then display the html code below
Line 6 – the html that I want shown if Line 5 is true
Line 7 – add 1 to the post count and loop it until 45
All of the code that I created went under the normal Wordpress post loop. See below:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="videoitem">...</div> <?php endwhile; else: ?> <?php endif; ?> right here |
The end result now looks like this:

If you want to see it in action, go here.
Testing Two New Wordpress Plugins
Last night I received and email from HostGator saying that one of my blogs had been temporarily disabled due to excessive CPU usage. They recommended installing WP Super Cache. After installing it on that site, I decided to test it here too. I also installed WP Widget Cache. I’m hoping that those two plugins will dramatically reduce page load time and cut down on the server load.
WP Super Cache: This plugin generates static html files from your dynamic WordPress blog. After a html file is generated your webserver will serve that file instead of processing the comparatively heavier and more expensive WordPress PHP scripts.
WP Widget Cache: A high-performance caching plugin for WordPress, and a plus for WP-Cache or WP Super Cache!
If you’re curious as to what plugins are installed on this blog, you can visit this page.
Display Google Ad after the first post
I don’t use Adsense on this blog, but I do use it on other sites of mine. I can’t remember where I found this, but I did save the code and instructions.
1. Create a file named “google-ad.php” containing your Adsense code.
2. Upload the file in the directory with your other template files.
3. Open your home.php template file and change it to the following code:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); $loopcounter++; ?> <?php if ($loopcounter <= 1) { include (TEMPLATEPATH . '/google-ad.php'); } ?> <?php endwhile; ?> <?php else : ?> <?php endif; ?> |
If the $loopcounter is less than or equal to 1, then it includes your google-ad.php which contains your Adsense code.
Wordpress Plugin – AdServe
I was just looking in to a better way to deliver the banner that you see at the top of the page. I started my search at the WordPress plugin page. I found the plugin “AdServe”. It looked like it would do just what I wanted.
AdServe is the advertising server for WordPress. You could setup your banner campaigns using different sized banners, set available impressions and count resulting clicks! Optionally AdServe links ads to blog users so that one could check the campaign results within the Dashboard!
* Unlimited number of advertisers, banners and zones.
* Advertisers have web access to real-time reports for their specific campaign.
* Launch a new browser when user clicks an ad, making it simple for them to return to your site.
The installation was easy just like most of the Wordpress plugins. After you activate the plugin, you can manage ads by going to “Manage > Ads”. Imagine that! LOL
After you have an ad setup, you’ll need to setup an ad zone. This is done by adding code into your template. The code in the instructions didn’t work for me. After doing some searching, I’ve found that the code below actually works.
<?php AdServe("zone_name"); ?> |
If you have a blog, this is an easy way to display banners with added features.
WordPress Version: 2.5.1
AdServe Version: 0.3










