wordpress Using Custom Fields

by in , 0

Dump out all custom fields as a list

<?php the_meta(); ?>

Display value of one specific custom field

<?php echo get_post_meta($post->ID, 'mood', true); ?>

"mood" would be ID value of custom field

Display multiple values of same custom field ID

<?php $songs = get_post_meta($post->ID, 'songs', false); ?>
<h3>This post inspired by:</h3>
<ul>
	<?php foreach($songs as $song) {
		echo '<li>'.$song.'</li>';
	} ?>
</ul>

Display custom field only if exists (logic)

<?php 
    $url = get_post_meta($post->ID, 'snippet-reference-URL', true); 

	if ($url) {
	    echo "<p><a href='$url'>Reference URL</a></p>";
	}
?>

Reference URL

Leave a Reply