Using Custom Fields

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

Source

No related posts.

Comments are closed.

Request a Quote
  1. (required)
  2. (valid email required)
 

cforms contact form by delicious:days

Request a Quote