Wednesday 20 November 2013

How To Get Value Of A Specific Custom Field In WordPress

Important: All scripts hosted on widcraft.googlecode.com don't work anymore because Google has blocked that SVN repository.
WordPress allows us to add custom fields to a our posts, which adds extra information, which is known as meta-data. But we can do a lot more there custom fields. For example, I embedded a video in this post with the use of custom fields.

More of newbies gets confused when it comes to use custom fields. But they're not that complicated, at least the basic use of these fields.

Here's how you can get value of a specific custom field in your WordPress posts:

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

"mood" would be ID value of custom field. That was simple.

And here's how to display custom field only if exists:

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

    if ($url) {
        echo "<p><a href='$url'>Reference URL</a></p>";
    }
?>
Important: Check our new website TricksPanda.com for WordPress tutorials, plugins and more.
 
Powered by Blogger.