How to show AdSense units between posts in WordPress?

How to show AdSense units between posts in WordPress?The most commonly used style for the main index page of a WordPress blog is showing up a list of the posts-excerpts or headlines. Most of the bloggers like to use a paged-navigation under it to scroll through the older posts index (thanks to WP Page Navi). We can not call such layouts the magazine-style layouts completely.

On such a layout of a WordPress blog, one of the best idea to merge ads without losing the effect is showing the ads in the post-index i.e. between the posts-excerpts in the index. As the bloggers around the globe use Google AdSense in plenty, I’m going to explain this with the AdSense units, but you can use ad-units of other advertising programs also. Lets see, how to bring it on without using a plugin.

The file responsible to handle this index page in the background in WordPress theme directory is index.php. All we need to do is to modify the index.php file of our theme.

Note: Before proceeding, make it sure to backup your full WordPress theme.

  1. Edit the index.php file of your theme by going to WordPress Dashboard » Appearance » Editor » Main Index Template
  2. The WordPress index.php file displays the post-index by running a loop function, which looks like the below code:
    <?php while(have_posts()) : the_post(); ?>

    Locate the end of this loop i.e. <?php endwhile; ?> (it completely depends on how your theme is coded, so try to locate the perfect loop enclosure while doing the changes).

    Tip: If you are not able to find that php code, try searching for the keyword “while” in the index.php. You’ll definitely encounter the required code.

  3. Paste the below given code just before <?php endwhile; ?>:
    <?php if (++$PostCount == X) {?>
      <div style="margin: 10px 0;">YOUR_AD_UNIT_CODE</div>
      <?php } ?>
    <?php $postcount++; ?>

    Replace X in the above code with the number of posts after which you want to display the ad unit. Replace YOUR_AD_UNIT_CODE with the code of your AdSense or the other advertising program’s ad unit code.

Similarly, you can display ad units of different kind and dimensions by using the code like this:

<?php 
  if (++$postcount == 2){ ?>
    <div style="margin: 10px 0;">AD_UNIT_CODE_1</div>
  <?php } elseif (++$postcount == 4){ ?>
    <div style="margin: 10px 0;">AD_UNIT_CODE_2</div>
  <?php }; $postcount++; ?>

Code updated on August 16, 2011

Replace AD_UNIT_CODE_1 and AD_UNIT_CODE_2 with your ad units code and modify the $postcount value according to your need.

Discussion • 18 Comments so far
  1. Saqib

    Awesome. I am trying it out on my site but I am not able to find in index.php. Where more should I look for?

    • Rahul Arora

      Hey Saqib. Try finding the keyword “while” in index.php. I’m sure you’ll be able to find < ?php endwhile; ?> and to do that correctly with FreshLife.
      Regards.

      • Saqib

        Got it finally. Thanks. And let’s say I want this in each category page also, then i need to follow the same procedure but make this changes in archives.php, right?

        • Rahul Arora

          Welcome. Yes, you need to do the same thing with that file too for that.

  2. Ankur

    thats useful method. trying it on my site .

    • Rahul Arora

      Hey Ankur. You are welcome :)

  3. Ankur

    its working for me. thx so much,
    any way that i can place the add multils times i.e after 3rd and 5th post

    • Rahul Arora

      Yes. I explained it in the end of the post. In the last code section in the post, you can modify $postcount values to the number of post – after which you want the ad unit to appear – according to your need.

      • Ankur

        I am following the code at end but for some reason it doesnt work. It only shows me add after 4th post ( and not after 2nd too). its simple if else condition, dont know where I am going wrong.

        Here is my code:

        ADD 1 CODE

        ADD 2 CODE

        • Ankur

          the code is not appearing in your comments :

          here its again :

          !–(so that it displays on comment–
          Add1

          Add2

        • Rahul Arora

          Sorry, the code snippet won’t display here in comments. You can paste the link of screenshot carrying code.

        • Rahul Arora

          Modified the code a bit. I’m sure it will work for any template now.

  4. Ankur

    well, I got it working now. implemented this on my website now :)

    • Rahul Arora

      Pleasure that you find this article useful.

  5. Raheel

    Thanks for this very useful info… i will try this right away.

  6. CheapO

    Hi,

    I do not have this in my index file. I have looked up a couple other help items and had similar problems. Is this due to the theme I’m using? For some reason, they coded in a different way. I’m using Suffusion theme on wp.

    thanks

  7. Cheryl

    Thanks for sharing all this info. I am fairly new to the blogging world and am learning loads.

  8. Rabin

    Thanks bro..I was actually looking for this..Will try in my blog..

Leave a Reply

* marked fields are required