Sometimes a site may want a “featured” section with some text and an image. If you’re using custom fields to achieve this, you may want a way to keep an empty value to display. Meaning, sometimes you don’t always add the image but if in the template you have the code to automatically pull in the image custom field, then you’ll either get alt text in place of the image or a big red ‘x’. Here’s a small snippet of code that will return nothing if nothing is there.
Just place the following code where you would normally place your custom field code:
1 2 3 4 5 6 7 8 | <? php $values = get_post_custom_values("Your-Custom-Field-Value"); if (isset($values[0])) { ?> <img src="<?php bloginfo('site_url'); ?>/wp-content/uploads/>?php $values = get_post_custom_values("Your-Custom-Field-Value"); echo $values[0]; ?>" alt="<?php the_title(); ?>" /> <? php } ?> |
If you write a post that doesn’t have an image, you won’t get any of those pesky errors. Thanks goes to authors on the WordPress forums for their efforts in helping folks who aren’t as well-versed in PHP coding as we’d like to be.
Defy the Party and leave your mark!
There have been no comments yet. Or were there? Maybe the Comment Dictator is controlling the masses by withholding your comments.