How To Alternate Ads Between Posts on Wordpress
On some of my blogs on the front page, I alternate ads after the blog posts. It is a useful way to blend in ads inside your content on the front page. As far as I know there is no plugin that does it like this, so I had to custom code it, but I’ll tell you how, its not too complicated.
The first thing you need to do is go into the wp-includes folder. Inside go to the general-template.php file (in older versions it will be called something else.)
Inside you will create a PHP function. In this format
function get_advertisement() {
echo '
****AD CODE GOES HERE****
';
}
You will cut n paste the code for your PPC advertisment after the echo ‘ part. For some reason wordpress shows this ‘ differently then it should, so don’t cut n paste this code and use it. Just type it in through your keyboard instead so you get the right ‘ character.
What you acheived is you created a function called get_advertisement. Everytime it is “called” somewhere else it will display what is inside that echo part. Echo in PHP means to output to the display. One thing to keep in mind is if there is any ‘ in your PPC ad code you will need to do to put the backslash before it like \’ this or else you will get a PHP error.
Now the next step is to go to your index.php file inside your themes folder(for example, if it’s default them then default folder).
Open the file and find the following code:
<?php while (have_posts()) : the_post(); ?>
Add this line of code before it:
<?php $count=0;?>
Next find this line of code:
<?php endwhile; ?>
Add this line of code before it:
<?php if ( $count % 3 == 1 ) : get_advertisement(); endif; $count++; ?>
This will cause a loop function and alternate the showing of the ad
inside the get_advertisement function. You can modify the count % 3 == 1 part to different numbers. Changing the numbers will modify on which blog post the very first ad is shown, and how many blog posts between each ad.





April 16th, 2009
I almost exactely developped the same for my personal use. Nice to see you sharing it dude!
April 20th, 2009
Great code snippet. It works perfectly on my blogs. Thanks!
April 26th, 2009
Great, thanks for these tips. Your blog has been very helpful and interesting to peruse, keep it up!
April 26th, 2009
u tell me a great tips. now i can place ads between posts.
plz keep it up and tell me more great tips.
thx