Using Custom Fields
Posted by Alius Designs on Oct 15, 2011 in Blog, Wordpress | 0 comments
While working on a project for a customer, needed to use custom fields, came across a few useful snippets:
Dump out all custom fields as a list
<?php the_meta(); ?>
Display value of one specific custom field
“companyName” would be ID value of custom field
<?php echo get_post_meta($post->ID, 'companyName', true); ?>
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>";
}
?>
No related posts.




