Have you ever tried to insert advertisements (or any bit of code really) into the WordPress post loop, then found that it will insert the banner after each post?   It really depends on what you are going for, but this usually will not be an ideal solution to placing advertisements between posts on your homepage.   If you’d like to place something only after the first post in the WordPress post loop, here is a quick hack you can do to tell WordPress to only display it after the first post.

Simply go to your themes homepage and look for the following code:

<?php endwhile; ?>

Immediately before this code, place the following code:

<?php if(!$show_ads){ ?>
Insert Code Here
<?php $show_ads = 1; } ?>

Obviously you’ll want to replace Insert Code Here with your code.  Told you it was easy!

Digg This! | Stumble it! | Add to Del.icio.us | | Print This! |

It has been awhile since I’ve done a batch of WordPress Talk links, so here are a few WordPress links for you to enjoy:

Digg This! | Stumble it! | Add to Del.icio.us | | Print This! |

This guest post was written by Leland of Theme Lab, where he has released over 50 WordPress themes. In addition to themes, Theme Lab also provides some WordPress guides. If you have WordPress knowledge and are interested in writing a post for Hack WordPress, please contact us.

In this guide you’ll learn how to display Adsense on just your first post within the Loop. Sure, there may be plugins that will do this for you. This guide, however, will use code examples to accomplish the same thing by editing your WordPress theme.

The first step is to open up your index.php file in your theme editor. Find the following line:

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

Just above that, insert the following like this:

<?php $i = 1; ?>
<?php if(have_posts()) : ?>

Now, scroll down a bit until you find this line:

<?php endwhile; ?>

Insert the following above it, like so:

<?php $i++; ?>
<?php endwhile; ?>

The final step is to insert your Adsense code. Locate where exactly you’d like it within the Loop, and place it between a conditional tag like this:

<?php if ($i == 1) { ?> [YOUR ADSENSE CODE HERE] <?php } ?>

And that’s all you have to do. You can be creative with this code as well. You could add a certain css style class to the top post in your Loop. It doesn’t even have to be the first either, as you can just change the number in $i == 1 to whatever you want. This same method can be used in other archive templates such as archive.php.

You can display ads with WordPress in other ways as well, including plugins such as WhyDoWork Adsense Plugin (formerly Shylock Adsense).

Digg This! | Stumble it! | Add to Del.icio.us | | Print This! |

One of my favorite parts about using WordPress for my blogs is getting to work with PHP code, which I find to be much easier to write/hack.   For those that are shy around code, it really isn’t that difficult to get ahold of the basics of PHP, so WordPress is the right place for you. 

One great area to start is learning how the WordPress loop works.  This is a basic function of blogging used to display the most recent X number of posts on your blog’s homepage (for traditional blogs).   Rather than go into to much detail here, I’d like to point you towards a new post by Themelab which is designed to be the Utlimate Guide to the WordPress loop

This post definitely lives up to its name and goes beyond just showing you how to do something.  It actually explains how and why it works, and includes screenshots with many of the examples.   If you have any interest in learning about the WordPress loop you may want to read through this post and/or bookmark it for future reference. 

Digg This! | Stumble it! | Add to Del.icio.us | | Print This! |