How to add a Google AdSense link unit to the WordPress Loop

I am going to describe to you how to add a Google AdSense link unit to your WordPress Loop and stay within the (current) limitations of no more than three link units per page – and we will do it without a plugin.

I know that there are plugins available that do this for you, but why do with a plugin what can be done very simply within your theme? I’ve mentioned this before, that many folks (including me) tend to “overuse” plugins for simple things. I’m trying to get back to a “less is more” philosophy and using plugins when necessary and avoiding them when they are not.

The objective here is to do a count of how many posts have been displayed in the WordPress Loop so far.  If that count is 3 or less, then we will add a link unit, and for posts 4 and higher, we will not. “The Loop” is used by WordPress to display each of your posts. For the purpose of this example, we are only going to be talking about theme files that display more than one post (or excerpt) as opposed to a single post.

Fortunately, WordPress has a built in way to count the posts as they are displayed in the loop by using $wp_query. The $current_post property tells us the index of the post being displayed. In other words, it conveniently tells us how many posts have already been displayed. (Incidentally, $wp_query is used a lot throughout WP for a number of things. This property is only available in $wp_query during the loop.)

(Note: Before you make any changes to your theme files, you should make a backup of your theme.  That way you can roll back should anything go wrong.)

Start by editing your theme’s index.php file. (If your theme uses a home.php, and this file contains a WordPress Loop, i.e. it’s not a static home page, you will probably want to start with this file instead.)  You can edit these files on your local computer and upload them via FTP, or you can use the Theme Editor that is built into WordPress. To use the Theme Editor, go to the WP admin, select the Presentation tab, then the Theme Editor subtab. Then select “Main Index Template” from the list of files.

The section that we are concerned with editing is within the Loop. Look for the following to indicate where the Loop begins:

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

Now you need to find where the Loop is outputting your posts. For me, I have an <h1> tag for the title output, <?php the_title(); ?> then the content is written using <?php the_content(__(‘Read more &raquo;’));?>

I want the Google AdSense link unit to come between the title and the post. So between these two items we will place the following (Note: Make sure this comes after your </h1> tag for the title, or whatever heading you use. Otherwise your link unit will be part of this heading.):

<?php  if ($wp_query->current_post < 3) { ?>
Your Google Adsense link unit script code goes here.
<?php } ?>

Using this, a Google AdSense link unit will be placed in only the first three posts displayed on the page regardless of how many posts you display. This will keep you within the Google AdSense guidelines.

From here, you can experiment with formatting and display if you wish. Use a vertical rather than horizontal link unit. For a vertical link unit, you could wrap it in a <div style=”padding-left: 16px; padding-bottom: 8px; float: right;”> link unit code here </div> to have the post wrap around it.  Use your imagination. The important thing here is that you are now able to restrict the number of link units at the maximum number allowed.

Comments

  1. Lisa says

    Very nice way to automate the process and learning how to hardcode this information in. Usually I stick to plugins (but this slows things down!). Great idea though as this will speed things up versus the traditional plugin route.

  2. Karen Hayes says

    i love google Adsense, it enables me to earn money on the websites and forums that i have put up several years ago. if you got tons of websites, you can earn a lot from Adsense alone

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>