Christian Little

Information on Everything!

WordPress Hack: Displaying Images Next to Posts on the Homepage

By Christian Little • Sep 14th, 2008 • Category: Wordpress

Over the years I’ve had a lot of people ask me how to do this with WordPress, as just about every blog I’ve run displays some sort of image next to the posts on the homepage. It’s actually surprisingly simple to do, and there are several ways to go about doing it. So the code I’m showing today is just a very simple version, there are plugins available that do this but I like having direct control of the coding.

So the first step is to go into your WordPress Admin, then click on Design, then click on Theme Editor. This lets you edit the PHP/HTML coding of your WordPress theme (NOTE: Your theme files need read/write access for this editor to work, otherwise you have to edit the files directly through FTP or some file manager that your hosting company gives you).

Next you need to open the specific template file, which in this case will be the ‘Main Index Template’ (it may have a slightly different name on older versions, pre-2.0 WordPress I believe it was just called ‘Homepage Template’).

While I’m on the topic of older WP, this probably won’t work with anything pre-2.0 WP. I can’t remember if the older versions allow for Custom Fields or not, so upgrade to the latest version of WordPress before putting in this hack.

Once you’ve loaded the template, look for the code that displays the posts on the homepage, usually within a special WP loop that starts with this code:

if (have_posts()) : while (have_posts()) : the_post();

That’s the start of the WordPress loop, a built-in set of code that tells WordPress to loop through the last 10 posts on your blog (or however many you have set it to in your Options).

Once there, you simply need to find the code that creates the link to each post (which is just a basic anchor link that called the the_title() and the_permalink() functions. Replace that specific line of code with the following:

(If you are reading this post in an email, the above block of code probably won’t show up as I’m using an iframe to display it. So you’ll have to come to my site and view this posting to get the PHP and HTML code to do this hack).

What this code does is tell WordPress to display an image next to the post title on the homepage if the Custom Field “Image” has been declared on that specific post. If there is no “Image” custom field set, it just displays the anchor text without the image. That makes this safer than most plugins, as just about every plugin that offers this assumes you have set an Image and will display the IMG code in HTML, which can result in several broken images on your site.

So once you’ve put this code in, create a new post on your blog to test it. Simply create a test post, add some content, and then scroll down to the “Custom Field” section and add “Image” as the Key, and the filename as the value, example:

Once you post it, it will display whatever image is stored in “/images/wordpress.jpg” if you use that specific example. I use the “/images/” folder on my site for this, which has about 20-30 different icons for posts now.

NOTE: Please be sure to check the HTML coding that I’ve given you above and edit it to fit your site. Right now it will encapsulate it all in an H3 tag and use the “/images/” directory as a base. You need to change this information if it doesn’t look on your site.

NOTE 2: Don’t delete the ” that you see sitting on a line by itself. If you read through the code above it you’ll realize it has to be there to complete the URL declaration for the image code. If you take it out, you will get some HTML errors.

As I mentioned earlier, there are several other methods of doing this and plugins available, so here’s a few other ways people have demonstrated how to to this:


Tagged as: ,

Christian Little is a web monkey and owner of this website. Aside from blogging about webmastering, SEO, and marketing, he spends his time with his family, running too many websites, playing counter-strike, and provides SEO consulting for a few select clients around the world.
Email this author | All posts by Christian Little

8 Responses »

  1. This is very useful I will definetly try this on my new blog. Hoping it will work. Though I like the WP extensions I also like to do some work on my own and reshape the WP to my needs. This will definetly help me to improve my site.
    Keep the good and useful WP coding&hacking tips coming, we’ll be reading …

    Rate this comment:
    2.5
  2. Glad to hear it. If you have something specific you want help with regarding hacking up wordpress let me know and I’ll show you how to do it.

    Rate this comment:
    2.5
  3. Thanks for the information. Your blog is added to your bookmarks. Develop.
    Your reader.

    Rate this comment:
    2.5
  4. Fantastic. care to share your sources :) ?

    Rate this comment:
    2.5
  5. Finally someone who can write a good blog ! . This is the kind of information that is useful to those want to increase their SERP’s. I loved your post and will be telling others about it. Subscribing to your RSS feed now. Thanks

    Rate this comment:
    2.5
  6. Great articles & Nice a site?.

    Rate this comment:
    2.5
  7. : P … Just Telling: P: D

    Rate this comment:
    2.5
  8. Brilliant and well deserved!

    Rate this comment:
    2.5

Leave a Reply