Skip to content

Instantly share code, notes, and snippets.

@sambody
Created June 13, 2013 13:19
Show Gist options
  • Save sambody/5773592 to your computer and use it in GitHub Desktop.
Save sambody/5773592 to your computer and use it in GitHub Desktop.
WP: Custom fields output. Input: use page editor metabox
/* simple output of value (url) custom field with key "post-icon", in your page/template.
*/
<?php echo get_post_meta($post->ID, 'post-icon', true); ?> // 'true' means string instead of array
/* use 2 */
<img src="<?php echo get_post_meta($post->ID, 'post-icon', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
/* use 3 - outside of the loop (eg in sidebar or...), make new loop */
<?php query_posts('showposts=10&offset=0'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'post-icon', true); ?>" alt="Icon for Post #<?php the_ID(); ?>" />
</a>
<?php endwhile; endif; ?>
/* use 4 - display if it exists */
<?php $image = get_post_meta($post->ID, 'url', true);
if($image) : ?>
<img src="<?php echo $image; ?>" alt="" />
<?php endif; ?>
/* use 5 - multiple values */
<?php $songs = get_post_meta($post->ID, 'songs', false); ?> // false to get array
<ul>
<?php foreach($songs as $song) {
echo '<li>'.$song.'</li>';
} ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment