Add a Google AdSense link unit to the WordPress Loop

January 5, 2008 | Filed Under WordPress | 1 Comment

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 count how many posts we have displayed in the WordPress Loop so far and if it is 3 or less, then we will add a link unit, if it is more than three, 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 themes 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.

Add a digg count to your WordPress posts

December 29, 2007 | Filed Under WordPress | 2 Comments

One of the things that originally attracted me to WordPress was the extensibility of plugins. But I have more recently come “full-circle” in my thinking seeking to do more without the use of plugins. Not that I don’t like plugins, but there are some simple tasks that are more efficient to do directly. Too many plugins unnecessarily slow down your blog, in my opinion.

So here is a way to directly place a Digg counter/link neatly aligned in your blog posts using CSS.

For this, I chose the “Digg This” button that you can see at the upper left of this post. This is called via Digg’s own javascript using this:

<script src=”http://digg.com/tools/diggthis.js” type=”text/javascript”></script>

I wanted to have this neatly wrapped in the upper left corner of the content. This is easily accomplished via CSS in a div tag:

<div style=”padding-right: 8px; padding-bottom: 4px; float: left;>
<script src=”http://digg.com/tools/diggthis.js” type=”text/javascript”></script>
</div>

Where you place this is up to you depending on your theme. For me, this went in the single.php file just before WordPress outputs the post content with <?php the_content(); ?>.

You can easily add this to your theme using the WordPress theme editor. Go to the WP admin panel and select Presentation > Theme Editor. Using the Theme Editor, you can select the necessary theme file(s) from the right hand side. The single.php template is indicated by “Single Post.”

From my bookmarks:

SearchStatus

A Firefox SEO Toolbar Extension - great for checking on your site(s). Check Google PageRank, Google category, Alexa, Complete.com, incoming and backward links for Google, Yahoo!, and MSN.

View my other bookmarks here.

From my bookmarks:

Darren’s Script Archive

chatrooms, image galleries, applets, advanced php/mysql systems, website scripts, flash xml, chat, and more - most scripts are $5

View my other bookmarks here.

From my bookmarks:

Dodo’s Scripts Collection

Dodo’s is a great collection of php scripts and code. Very usable gallery, upload, quiz, mailer, random generator, and other scripts.

View my other bookmarks here.