Created
May 31, 2012 05:29
-
-
Save markreale/2841293 to your computer and use it in GitHub Desktop.
Conditionally Displaying Custom Fields with WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************************************************** | |
* * | |
* Conditional statement to retrieve content based on the * | |
* existence of a Custom Field. * | |
* If the Custom Field exists, display the content of the * | |
* Custom Field (can be adjusted to display alternate content). * | |
* * | |
****************************************************************/ | |
// Check for existence of content in that custom field | |
<?php if (get_post_meta($post->ID, 'image_url', true)): ?> | |
// if there is content, do something with it - in this case, display an image | |
<img src="<?php echo get_post_meta($post->ID, 'image_url', true) ?>" alt="Your alternate text" /> | |
// if there is no content | |
<?php else: ?> | |
// display some alternate content, in this case, a placeholder image | |
<img src="http://placehold.it/215x215" /> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment