Skip to content

Instantly share code, notes, and snippets.

@markreale
Created May 31, 2012 05:29
Show Gist options
  • Save markreale/2841293 to your computer and use it in GitHub Desktop.
Save markreale/2841293 to your computer and use it in GitHub Desktop.
Conditionally Displaying Custom Fields with WordPress
/****************************************************************
* *
* 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