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.
- Edit the index.php file of your theme by going to WordPress Dashboard » Appearance » Editor » Main Index Template
- 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.
- 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.
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?
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.
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?
Welcome. Yes, you need to do the same thing with that file too for that.
thats useful method. trying it on my site .
Hey Ankur. You are welcome :)
its working for me. thx so much,
any way that i can place the add multils times i.e after 3rd and 5th post
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.
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
the code is not appearing in your comments :
here its again :
!–(so that it displays on comment–
Add1
Add2
Sorry, the code snippet won’t display here in comments. You can paste the link of screenshot carrying code.
Modified the code a bit. I’m sure it will work for any template now.
well, I got it working now. implemented this on my website now :)
Pleasure that you find this article useful.
Thanks for this very useful info… i will try this right away.
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
Thanks for sharing all this info. I am fairly new to the blogging world and am learning loads.
Thanks bro..I was actually looking for this..Will try in my blog..