Conditional Display of AdSense and other Ads in WordPress - Part II


In the previous post on Conditional Display of AdSense in WordPress, we had learnt to display Ads in sidebar and depending on which page is being called, the Ad will either display just once or more than once. In this post, we will learn about displaying ads on the main page but selectively.

We will learn to display ads either between certain posts, or before 1st post and after 4th post or other possible combinations, whichever suits your requirement. The logic is fairly simple and with very minimal modification you can tweak it to meet your needs.

:idea: It’s recommended that you make a backup copy of the files in your theme’s directory before you make any changes to them.

The logic behind the scenes

We first determine the display position [number] of posts on the main page. As soon as we know of a way to determine the positioning of the post, it becomes easy for us to display the ads wherever we want them to appear in the page.

Let’s understand “The Loop”

In WordPress, there is a small piece of logical code which displays each of your posts on the page. This is called “The Loop”. In general:
The loop starts here:

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

and ends here:

< ? php endwhile; else: ?>
< ? php _e('Sorry, no posts matched your criteria.'); ?>
< ? php endif; ?>

Since our focus area in this post is just the main page, we will look at the code of the main file. It is called index.php [also known as Main Index Template].

On a closer look inside the index.php of your theme, you may find “The Loop” split in several lines.

In one line, you will see: [call it #main for later reference]

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

then in some other line you will see the second part of the loop:

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

and upon further scrolling down you will find the end of the loop:

< ? php endwhile; else: ?>
< ? php _e('Sorry, no posts matched your criteria.'); ?>
< ? php endif; ?>

Ok, Where do I make changes

Before we look at the changes that we will make in the code, let’s look at something else so that we can later make changes with ease.

Locate this line in the index.php file [You may use Presentation->Theme Editor->Main Index Template to check this]: [call this #1 for later reference]

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

Just few lines below the above line, you will notice couple of line which has the following PHP snippet in it: [call this #2 for later reference]

< ?php the_title(); ?>

The above is actually meant to return the title of the post. So, while this is put inside the loop and as long as there are posts in your WordPress, the system will continue to display each of them. There will also be another PHP snippet on the same page which looks like:[call this #3 for later reference]

< ?php endwhile; ?>

This one is the end of looping structure which displayed each of your post in the main page.

Let’s look at the code

Now that we know what code lies where, we are ready to make changes. First let’s keep a track of the post number being displayed on the page by adding a post number variable to our code [in #main]. The modified code will look like this:

< ?php $postnum = 0; if (have_posts()) : ?>

In the above line we have put a variable name $postnum and we have also assigned it a value of 0. Now, we shall increment the count of post number from 0 to 1 and so on along with each post which is being displayed. Here is the modified code [Refer to #1]

< ?php while (have_posts()) : $postnum = $postnum + 1; the_post(); ?>

The modified code above will increment the value of $postnum by one, each time a post is displayed from the available number of posts. This means that the first post on your main page is at the position of 1, second post is at 2, third will be at 3 and so on… I hope you are clear with this logic. We have adopted it so that you can display an Ad before the first post as well and not just after the second or the third post.

It’s time to put the Ad-code in the main file

Immediately after the code [we modified it above]

< ?php while (have_posts()) : $postnum = $postnum + 1; the_post(); ?>

insert the following code [I am pasting AdSense code, you may paste Ad code from other affiliate programs as well]:

<?php
$a = 2;
$b = 3;
$ad_code = "<script type=\"text/javascript\"><!–
google_ad_client = \"pub-1778120429997394\";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = \"468×60_as\";
google_ad_type = \"text_image\";
google_ad_channel = \"2152672412\";
google_color_border = \"FFFFFF\";
google_color_bg = \"FFFFFF\";
google_color_link = \"9BBB38\";
google_color_text = \"000000\";
google_color_url = \"E58712\";
//–></script>
<script type=\"text/javascript\"
src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>";
if ($a == $postnum) {
echo "<BR />$ad_code";
}
if ($b == $postnum) {
echo "<BR />$ad_code";
}
?>

Save the page, and you are ready to go!

:idea: In the above code, replace the adclient publisher ID with your’s and also change other parameters. Also note that, you can change the value of $a and $b and the Ad will be displayed just before that post number. If you put $a=0, the Ad will be displayed just before the first post’s title. Experiment and you shall learn. Also, you can introduce $c into this code and create an additional if loop [last few lines in the code].

That’s it!

Your comments, suggestions, feedback and questions are welcome. :)


RELATED POSTS:


6 Comments »

  1. HowTO: Place Ad Unit in Your Blog-Part II - Enblogopedia.com Said,

    December 23, 2006 @ 11:37 am

    [...] P.S. Tips above may not be correct in some cases, since it depends on the design of your blog! How Can I Put Ad Units Between Posts? The answer of this question is easy..and Raj posted a nice tutorial called “Conditional Display of AdSense and other Ads in WordPress” about it. Honestly I had some problems following his tutorial. Anyway, this is my code: As in Raj tutorial you create a counter and set it to zero before the loop start, then the counter should increment by 1 whenever one loop ends <?php $postnum = 0; if (have_posts()) : while (have_posts()) : $postnum = $postnum + 1; the_post(); ?> and since I don’t have a Single page template the condition and the code will look like <?php if (2 == $postnum && !is_single()) { ?> <br/>YOUR Ad Unit Code or Adsense Deluxe PHP command goes here! <?php } ?> As you can see here the Ad unit will appear after the second post. [...]

  2. WordPress K2: Google AdSense Under First Post Only | davemorg.org Said,

    March 23, 2007 @ 4:36 pm

    [...] AdSense block under only the first post on a Wordpress installation. While I found a good deal of information, I couldn’t find very much of anything that applied specifically to the popular K2 [...]

  3. Rod Said,

    May 17, 2007 @ 9:52 am

    Thanks for this post. I’ve been looking for this information for a fair while and all the other tutorials on how to do it just never seemed to work. This one, I’m happy to say, worked perfectly.

  4. Raj Said,

    May 17, 2007 @ 10:51 am

    Hello Rod,
    Nice to know that it worked for you :) and thanks for visiting my blog.
    Cheers!

  5. nathan Said,

    May 23, 2007 @ 10:32 am

    Thank you so much - I don’t need the AdSense code but I’ve been wondering for awhile how you’d add info between posts like this.

    Great jorb!

  6. Raj Said,

    May 23, 2007 @ 10:40 am

    Hi Nathan,
    Thank you visiting my blog and nice to know that the info on this post could be of some assistance to you!
    Cheers!

RSS feed for comments on this post · TrackBack URI

Leave a Comment