If you want to maintain a sexy blog adding images is a must. Here’s a function to add to your Wordpress blog to ensure that every post you write has an image, even when you’re writing on the fly and have no time to attach an image.
I came across this function while developing this new theme for Robot Forest. I had to ensure that every post had an image to the left of it, whether I had time to attach one or not.
Copy and paste the following code wherever you would like the image to appear:
<?php $postimageurl = get_post_meta($post->ID, ‘thumb‘, true);
if ($postimageurl) {?> <div><a href=”<?php the_permalink(); ?>” rel=”bookmark”><img src=”<?php echo $postimageurl; ?>” alt=”Post Pic” width=”200″ height=”200″ /></a> </div>
<?php } else { ?>
<div><a href=”<?php the_permalink(); ?>” rel=”bookmark”><img src=”<?php bloginfo(’template_url’); ?>/images/default.gif” alt=”Screenshot” width=”200″ height=”200″ /></a></div>
<?php } ?>
In the first line the word “thumb” represents the custom field. When writing a post simply upload an image and place its source URL in a custom field and name the field “thumb” (you can change the name in the php and use a different custom field name if you want).
The default width and height are set to be 200×200, but this can also be changed to work for your theme.
Near the end, where it says “/images/default.gif,” that’s the kicker. Upload a default image to your images folder, perhaps your logo or anything you’d like, name it “default.gif” (again, you can change this), and no matter what there will always be an image on your posts.