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 16-05-08@19:21

    Really nice mode, thanks for sharing!

  2. Muhammad Abbas 14-08-08@06:33

    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 14-08-08@23:40

    Thank you for your kindness.

  4. How To Edit And Rewrite Post Slug / Permalink In WordPress 2.5? – Walker News 02-12-08@00:18

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

  5. blued888 15-06-09@01:46

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

  6. Talha Kazmi 30-07-09@22:29

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

  7. erico 04-08-09@02:05

    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 05-08-09@13:02

    Just replace the echo line with image code.

  9. Hiro 12-04-11@08:33

    How can I do this displaying ads between comments?

  10. Hiro 20-04-11@16:47

    No one knows how to display ads between comments?

2014  •  Privacy Policy