Posted on 24 July, 2012 By 4 Comments

How to Add a WordPress Default Post Thumbnail Image Without a Plugin

 

A little over a year ago I wrote about a plugin that with set a default image for post thumbnails if one wasn’t set.

Post thumbnails were added to WordPress in version 2.9 so if your site was older than that, your older posts wouldn’t have one set (since they didn’t exist). And if you have a longer attention span than I do, you could always go back through your older posts and set a post thumbnail in the featured image section. Of course, the code for post thumbnails does need to be added to your template files first.

Since I test a lot of plugins, I often have too many installed on this site so I’m always looking for ways to reduce the number being used. I accidentally came across Justin Tadlock’s article ‘How to Define a Default Post Thumbnail‘, which contained the perfect solution for removing the Default Post Thumbnail plugin. I say accidentally because I was halfheartedly cleaning out my feed reader and wasn’t actually looking for a solution to this issue.

I use default post thumbnails on my archives template. Other locations on the site have images defined properly so this is where I added the new code. (I used Justin’s solution #2). I also uploaded my default image to the images directory for my theme as it was previously located in the uploads folder.

Here is Justin’s modified code that I added to my archives template so it would use my default image.

<?php
if ( has_post_thumbnail() )
the_post_thumbnail(‘thumbnail’);
else
echo ‘<img src=”‘ . trailingslashit( get_stylesheet_directory_uri() ) . ‘images/rainbow-150×150.jpg’ . ‘” alt=”Rainbow” class=”left frame” />’; ?>

It’s saying to use the post’s post thumbnail if there is one and if there isn’t one to use the rainbow image. The name of your image, the alt text and the class will differ.

And like that, I’m using one less plugin :-)

If you would like assistance adding code like this to your WordPress site, please contact me.

photo credit: thunderchild5


web hosting by media temple

Keep Reading:
Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.




4 Responses to “How to Add a WordPress Default Post Thumbnail Image Without a Plugin”

Leave a Comment
You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

CommentLuv badge

About

Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

Recent Comments

    • Chris Tusa: NovelSnippet.com is cool. It contains hundreds of books (and seems to be rapidly growing). The books are accompanied by ...
    • Dee: I drop a leave a response when I especially enjoy a post on a site or I have something to contribute to the conversation...
    • Kabsiha: Thanks for your great idea to hide the deactivated comment link in word press this would be the very useful post for who...
    • Pat Alexander: Well now these are really great. Makes it perfect for small sites....
    • Tracy G: I really don't know how to do it, glad you posted it here....

Wordpress Services

  • Installation, upgrades and maintenance
  • Conversion of existing html and css templates
  • Theme and plugin recommendations
  • CSS customizations
  • Troubleshooting and tweaks for unique situations
  • Customization for individual blogging goals and needs
  • Training and advice