Skip to content

How To Get Value Of A Specific Custom Field In WordPress

WordPress allows us to add some extra metadata to our posts with the help of custom fields. You can do a lot of amazing work with these custom fields, as well as most of theme developers use these fields in their amazing themes. Adding a custom field to your post is really easy. Just add the field name and content from your post editor and that’s it.

Now, here’s the most confusing part for the newbies. How to get the value of a specific custom field and display it in your post? The following snippet will show values of all the custom fields of the post:

<?php the_meta(); ?>

Here’s how you can get value of a specific custom field in your post:

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

The above code will get the value of “video” field. And here’s how to display custom field only if they exists:

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

    if ($url) {
        echo "<p><a href="$url">Reference URL</a></p>";

Leave a Reply

Your email address will not be published. Required fields are marked *