Lego2012
12/13/2016 - 11:30 AM

Using Custom Fields

Using Custom Fields

<!-- http://perishablepress.com/press/2008/12/22/wordpress-custom-fields-tips-tricks/ -->

<!-- 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>";
  }
?>