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).
One thing I am really excited about is that Gravatars are really starting to gain popularity now that Automattic has purchased and is supporting them. And now that support is built right into WordPress, it opens up a lot of options for WordPress users.
The most common place you’ll find a Gravatar is usually with an individual comment to help comments stand out. Another place you will sometimes see them is in the sidebar, like we have it setup here at Hack WordPress. One thing, however, that people are slowly coming around to is using Gravatars with blog posts to identify the author of the post. This is something that is a great idea for a multi-author blog and something I’ve considered doing on this website.
So, how would you go about setting up Gravatars to display with each individual post? Over at ThemeShaper, Ian Stewart recently shared an easy way to do this. You just need the following code:
<?php echo get_avatar( get_the_author_email(), '64' ); ?>
When used, WordPress will match up the e-mail address associated with the post author to determine what Gravatar to use. The 64 is the size (pixels) of the Gravatar.
Great find Ian!
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.
Sometimes you may not want your search results to be limited by the confines of the standard WordPress Loop. This is a quick code hack to allow a search to return unlimited results, altering the standard WordPress Loop by using a custom query. You can do this in a few different ways. If you have a search template, in search.php you can simple add the following line of code above your Loop.
Find:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
Add:
<?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
Make sure you put this code in your search.php only, unless you want unlimited posts on your index or archive pages. The -1 you see can be changed to any positive integer to limit the posts to a number you choose as well.
If you don’t have a search.php in your theme, the next level down in the Template Hierarchy is your Main Index Template, or index.php. You can use a conditional tag for the same effect.
For this we’ll use the same code as above, except wrap it in the is_search() conditional tag, like so:
<?php if (is_search()) { $posts=query_posts($query_string . '&posts_per_page=-1'); } ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
You can use this technique to change the standard Loop limitations of archives, categories, tag pages, and even your main index template - but it would probably be easier to simply change your reading settings for that.
This guest post was written by John of The WordPress Expert, where he writes about WordPress tips, services, themes, plugins, and more. If you have WordPress knowledge and are interested in writing a post for Hack WordPress, please contact us.
The links importer allows you to import links from an OPML file. In previous versions of WordPress, the links importer was located under the Blogroll section. However, with the admin menu redesign in WordPress 2.5, the links importer isn’t even on the menu anymore!
WordPress 2.5 still has a links importer, but you have to go through a few extra steps to get to it:
- Login to your WordPress admin
- Click “Write”
- Click “Link”
- Look under the “Related” section on the right
- Click “Import Links”
From there you can use the link importer like you would in WordPress 2.3.
Funny that the links importer still uses the term “Blogroll,” even though it’s been changed to “Links” in other parts of the administration. ![]()







