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-150x150.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