Walker News

Display Adsense Ads Between Posts In WordPress

The New Blogger Adsense widget has an option called “Show Ads Between Posts“.
So, how could we display or show Adsense ads between posts in WordPress theme? No worry. There are only two steps to go:
  1. Edit the index.php (one of the WordPress theme file)
     
  2. Add this PHP snippet right after the the_content function call
    <?php
           $postcnt++;
           if (($postcnt % 2) == 0){
              echo 'Your Adsense Code Here';
           };
    ?>

    You can change the modulus (e.g $postcnt % 2 == 1 or $postcnt % 3 == 1) as you like.

In this WordPress theme, these are the changes made to display a banner ads format after the 3rd post and a big square ads format at the last post of WalkerNews.net index page:
<?php
        $postcnt++;
        if ($postcnt == 3){
            echo 'Banner format Adsense code';
        } elseif ($postcnt == 7){
            echo 'Big square format Adsense code';
        };
?>



Custom Search

  1. coder May 16th, 2008 7:21 PM

    Really nice mode, thanks for sharing!

  2. Muhammad Abbas August 14th, 2008 6:33 AM

    nice post dude, i was looking for something exactly like that. I’ve got very little experience with PHP, although i am a good coder in C++ and Java , so changing that code to my need is gonne be easier now.

    Thanks though.

  3. Walker August 14th, 2008 11:40 PM

    Thank you for your kindness.

  4. How To Edit And Rewrite Post Slug / Permalink In WordPress 2.5? – Walker News December 2nd, 2008 12:18 AM

    [...] WordPress automatically removes any punctuation marks from the post title for permalink, it can’t [...]

  5. blued888 June 15th, 2009 1:46 AM

    Thanks for this! Was just wondering how to do ads between posts.

  6. Talha Kazmi July 30th, 2009 10:29 PM

    Hi,
    Thanks a lot you have done a really good job…

  7. erico August 4th, 2009 2:05 AM

    Does it work with an image instead of adsense? I want to place a comic strip instead of an ad. Could you explain how to call the image?
    Best regards
    Erico

  8. Walker August 5th, 2009 1:02 PM

    Just replace the echo line with image code.