I want to output the metadata from a custom field in WordPress post.
On this page if WordPress codex I found the following instruction:
To fetch meta values use the
get_post_meta()
function:
get_post_meta($post_id, $key, $single);
I am trying to do it this way:
<?php
get_post_meta(1, 'Currently Reading',true);
?>
But nothing gets output in the browser.
What’s the proper way to output the content of the custom field?
The easiest way to do this is this:
On your post or page editor, you can go to “Screen Options” in the top right corner and check the box to display “Custom Fields”. This will allow you to see the meta keys available. Just copy the name of the meta key into your
get_post_meta
call in the spot above where it says “your_meta_key”. Don’t change the$post->ID
as that is global.Taken from that page linked
so you’d need to access it through the
$meta_values
return object.Like so:
get_post_meta(1, 'Currently Reading',true);
will only get the values, you need to store it somewhere and output it properly. One way to do this is to store the function return values into a variable like so:<?php $custom = get_post_meta( 1, $key, $single ); ?>
Then you can output it with a
print
orecho
like so:echo $custom;
Something to note, try using a value
$post_id
for the first argument. This will grab the current post id.