Display Multiple Ads After Any Post Number

Sometime we may need to add an advertisement or adsense ads after our first post. That is quite easy to do. However, how if we want to add multiple advertisements after certain number of posts? See how it can be done..

  • First open your themes index.php file using any of your favorite editor.
  • Find the following code of line:

<?php if(have_posts()) : ?>

  • Copy the code below and paste it before the above code.

<?php
$postnum = 1;
$showads1 = 1;
$showads2 = 2;
?>

  • Scroll more down the index.php page and find the following the code of line:

<?php endwhile; ?>

  • Same procedure, copy and paste the code below before the code above.

<!--Ads Code Start Here--!>
<?php if ($postnum == $showads1) { include (TEMPLATEPATH . '/ads1.php'); } ?>
<?php if ($postnum == $showads2) { include (TEMPLATEPATH . '/ads2.php'); } ?>
<?php $postnum++; ?>
<!--Ads Code End Here--!>

  • Finally, create two files named as ads1.php and ads2.php and paste any advertisement code into both the files. Not only advertisement actually. You can try to insert anything, really.
  • Make sure to put both files into the same directory as the index.php file.

The code above will display ads after the first and second post. By changing the $showads1=1 and $showads2=2 number into “2” or “3” will give different position of advertisement. Try with different combination to get anywhere you want the ads to appear.

Best example is that you can see after my first post, there are two rows of section (popular posts and subscription section).

Now, enjoy!

  • http://www.dailymorningcoffee.com Praveen Rajarao

    awesome, just what i needed…thanks.

  • http://makeityourringdiamond-engagementrings.com/ MakeItYourRing

    That was an awesome tip! Good luck with your work.

  • Christian Z.

    Thanks so much for this post. I’ve been looking high and low (mostly low, lol) for adding multiple ads in between posts for both my blogs GearCulture (http://www.gearculture.com) and 21 Gents (http://21gents.com). This solution was the simplest and I’m glad it also works like a charm. Bonus on using includes instead of adding more code to the page. It makes managing the ad codes so much easier.

  • http://wptidbits.com/ xaer8

    Glad they’re useful to you. Thanks!