Code Snippet
Using Custom Fields
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>";
}
?>
OMG. This is probably the most useful snippet I have ever come across, the amount of time I have spent in the past trying to remember this!
Thanks. I was just looking for the “Display custom field only if exists (logic)” today for a project and could not find an example that I could understand how to change for my specific case. This snippet is just what I needed.
The logic snippet worked wonderfully for me. Thanks.
One question though, why haven’t you closed the tag?
Thank you sooooo much! I adore your snippets!
Very nice quick and easy reference for custom fields. Thanks a lot!
Hi
These snippets are great. I was wondering if you could help me.
I am trying to match the title of a page to a custom field and display any posts that match that custom field.
Total newbie here to both php and wordpress.
Any help is much appreciated!
:)
I solved my problem with Custom fields, thanks
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.