Add Adsense between some Posts in WordPress
In WordPress on the frontpage you have normally a flow of posts.
I want to show you how to put any advertising link or Adsense code between one or more of this posts.
We are going to edit the index.php for this, so please make a BACKUP of this file before starting to change anything!!
We will do this by setting a post counter in front of the loop
$postno = 1
inserting the add code inside the loop
if ($postno == 3) do stuff
and make the postcounter count up one digit for every post
$postno++;
In Detail:
Take the index.php of your theme and find:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
place the counter start between this two lines like this:
<?php if (have_posts()) : ?>
<?php $postno = 1; ?>
<?php while (have_posts()) : the_post(); ?>
as we are going to place to add codes after the first and the third post we find the next:
<?php endwhile; ?>
and insert my suggested code just above this endwhile:
<?php if ($postno == 1) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. one replace with your code
</div><?php } ?>
<?php if ($postno == 3) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. three replace with your code
</div><?php } ?>
<?php $postno++; ?>
I styled this code to center on the page and give some space below, you are free to remove or change this div style.

There are some themes like Arthemia by Michael Jubel which use an other query for posts, so you will not be able to find the if (have_posts()) : part in there.
In a query like in that theme you should look for:
<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-27,-28&paged=$page&posts_per_page=5"); ?>
<?php $postno = 1; ?>
<?php while (have_posts()) : the_post(); ?>
and place the counter start before while (have_posts()) : the_post(); – I did this here already.
The code to call the add code and count up one by one (as before) is placed in the same way as in default theme before the next endwhile; after the div before, it should look like this:
<?php the_excerpt(); ?>
</div>
</div>
<!-- new code start -->
<?php if ($postno == 1) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. one replace with your code
</div><?php } ?>
<?php if ($postno == 3) { ?><div align="center" style="margin-bottom: 10px;">
AdSense Code below post No. three replace with your code
</div><?php } ?>
<?php $postno++; ?>
<!-- new code end -->
<?php endwhile; ?>
<div class="navigation">
happy coding…
Related posts:
- Exclude one headline post from frontpage but not if is paged
The problem: I got a WordPress site with feature headline,... - List some pages with content in Wordpress
To list pages in WordPress you normally take this: <?php... - List subcategories in category bar of arthemia theme
In the WordPress theme Arthemia theme by Michael Jubel is... - List one latest post per category on frontpage view
I will give you here a code to list only... - Google, Adsense, Googlemail und youtube so langsam heute
Heute ist mal wieder so ein Tag, wo Google nicht...
JW ist Freelance Webdeveloper
Website Questions? Please ask in Webdesign Forum
You can follow any responses to this entry through the
RSS 2.0 feed.
You can leave a response, or trackback from your own site.





