If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
As one of my previous tutorials on creating a dynamic slideshow from your posts showed, there are a lot of useful and interesting uses for Custom Fields. One use that I make of custom fields is to pull an image from a post and display it on my homepage, however I don’t always need something different for each post. This is where this method is a real time saver!
Of course, you could just add the same custom field for every post where you want the ‘default’ used, but why bother when we can make it easier for ourselves? The first example code below will show a basic method that can be adapted to your needs, and then the second example will develop that to demonstrate how I achieve my effect with images.
The Basic Code
The basic method will check if the desired custom field ‘key’ has no defined ‘value’, or if it is absent. If it is missing, then the default text is shown, otherwise show the ‘value’.
<?php
$var = get_post_meta($post->ID, 'My Custom Key', true);
if ($var == '')
{
echo "<p>I'm the default text!</p>";
} else {
echo "<p>" . $var . "</p>";
} ?>
Simple!
Default Image Display
If you take a look at my WordPress category page you will see that some of the images for the posts are the same. I achieved this by extending the above code to include a default image if I didn’t need an image to specifically demonstrate the post.
<?php
$thumb = get_post_meta($post->ID, 'Homepage Image', true);
if ($thumb == '')
{ ?>
First of all we store the ‘value’ defined for the ‘key’ Homepage Image for the current post in a variable called $thumb. We then check to see if $thumb is either blank, or if there is no Homepage Image ‘key’ at all. If either of these is the case, then we do the following:
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <img src="http://domain.com/images/defaultimage.jpg" alt="<?php the_title(); ?>"/> </a>
This will output an image that is on my server each time there is no custom field available.
<?php } else { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<img src="<?php echo $thumb; ?>" alt="<?php the_title(); ?>"/>
</a>
<?php } ?>
In addition to the if statement, we add an else. This says that if $thumb is not empty, ie it has a defined ‘value’, then output that ‘value’ in the image source, and make it a link to the post.
This saves me a lot of time when posting, as I don’t need to find the url of my default image and add it to the custom fields, I can just post straight away, knowing that an image is going to be there where I need it.


I was really scratching my head about this as I had posted asking how to do this exact thing on the WP forums but no one answered.
Thank you so much for this. Saved me hours
I used it in conjunction with TimThumb & Random File so I get resized random images for non-custom field images.
Regards
Karl
Actually, the code should use single quotes, not double quotes, in the second else in the basic code;
echo ‘whatever’ . $var . ‘whatever’;
I didn’t and my page wouldn’t display at all.