WordPress: inserting ads between two posts in post list

Last updated on July 24, 2010. Tags: , , ,

Aside from the static pages and posts, you can also insert ad units (Adsense, Chitika, etc) between any two adjacent posts in a post list page (category pages, search results, home page if configured to display list of post, tag page, etc). Somehow similar to inserting ads between paragraphs, the objective here to get the site visitor to notice the ad as s/he looks for a particular post in the post list.

To insert an ad unit between posts, use the instructions in the following sections as guide.

How to insert a single ad unit between posts

In the index.php file of your theme, look for the following lines of code:

<?php if (have_posts()) : ?>

Some themes (such as the WordPress Classic) do not have this simple code. In its place, they have a more complicated line:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

On the line above either of these codes (whichever is the one you found on your theme), insert the following:

<?php $var = 1; ?>

Then, look for the line:

<?php endwhile; ?>

... or if you cannot find it, look for:

<?php endwhile; else: ?>

... and insert the following code above it:

<?php if ($var == 1) : ?>
[ INSERT AD CODE HERE ]
<?php endif; $var++; ?>

In the above code, simply replace the  [ INSERT AD CODE HERE ] with actual code for your ad unit and it will appear below the 1st post (and above the 2nd post). If you want the ad to appear below the 2nd post or the 5th, just replace the 1 in $var==1 with 2 or 5 respectively.

In some themes, the index.php generates all the lists of posts. However, there are certain themes that have separate theme files for generating the search results (search.php) and archives (archive.php). If you want the ad to appear in these list pages, you need to repeat what you did to index.php to these two theme files. They have basically the same structure so if you succeeded in index.php, it should not be difficult.

Then, reload the post list (home page, category page, any post list page) of your blog and you shall see the ad unit appearing between the posts in the list. If your ad didn't appear or something went wrong, proceed to section 3: Precautions to possible problems.

Insert several ad units between posts

If you want more than one ad per list, you can use or argument (||) to specify the variables where you want the next ad to appear. For example, you want it to appear after the first post and after the seventh post:

<?php if ($var == 1 || $var == 7) : ?>
[ INSERT AD CODE HERE ]
<?php endif; $var++; ?>

In this code, ads will appear after the 1st post and the 7th post.  Refresh your post list and you shall see the ad units. If your ads didn't appear or something went wrong, proceed to the next section. Otherwise, you're work is basically done.

Precautions to possible problems

If your ad didn't appear, it's either you did something wrong or the ad provider (e.g., Google) is still trying to analyze the content of your site. Another possibility is that your theme has been programmed in a way that is more complicated than a typical WordPress theme intended for blogging;  you might not find the lines I mentioned or they work in a different manner. You might also be using WordPress versions that are far from 2.7.1 to 2.9.1; versions in which this code was tested.

Note that this tweak counts the sticky posts. Thus, if you have one sticky post and you have if ($var == 1), the ad will appear under the sticky post. If you have two sticky posts, the ad will appear between these sticky posts.

<?php endwhile; else: ?>

Posted by Greten on June 5, 2010 under WordPress tweaks

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by Ivan on 07.21.10 5:39 pm

    Hi,

    I think you made a mistake there. Setting the variable part should be placed ABOVE the first line of the loop, because it will set the variable to 1 every time it enters the loop.

    Anyway thanks, this worked for me with this little modification.

    Have a nice day.

  2. Posted by Greten on 07.24.10 10:02 pm

    Hi Ivan,

    Thanks for the correction. I studied it and already modified this post to incorporate your correction plus a few minor details I found while reviewing this technique.

  3. Posted by Arpita on 08.08.10 5:20 am

    Thanks for this Hack. It worked for me….

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.