I am trying to piece together a Metabox with 3 text editor fields for a custom post type.

The box is showing up and appears to be saving on the custom post type entries but I can't get the data to display on the single-[custom-post-type].php page.

Most recently I have tried the following to get the meta box data to display on the page:

global $post;
$meta = get_post_meta($post->ID, 'my-info', true ); 
if ($meta != '') {
    echo $meta
} else { 
    echo "Can't Display The Content";

I'm not sure if it's a problem with my Metabox creation/save that I can't get it to display. If anyone can point me in the right direction I'd appreciate it.

Here is a link to the code that I'm using for my Metabox if it helps:

Metabox Code

  • Any progress on that question? – kaiser Oct 22 '13 at 10:30

To show post type meta data on a single page template, I assume that you're in the Loop.

// Use get_the_ID() to get the ID via the API function
echo get_post_meta( get_the_ID(), 'my-info', true );
// You can also call it from the global, as the query refers to the current single page
echo get_post_meta( $GLOBALS['post']->ID, 'my-info', true );

If you're not getting any output, then you might want to check your complete set of post custom data:

printf( '<pre>%s</pre>', var_export( get_post_custom( get_the_ID() ), true ) );
| improve this answer | |

Use IDs of fields to get meta data of respective fields as following code.

global $post;
$meta = get_post_meta($post->ID,'myinfo-box1', true); // Use myinfo-box1, myinfo-box2, myinfo-box3 for respective fields
if ($meta != '') {
    echo $meta;
} else { 
    echo "Can't Display The Content";
| improve this answer | |
$m_meta_description = get_post_meta($post->ID, 'images_url',true);

echo 'meta box value: ' . $m_meta_description;
| improve this answer | |

Some times id is not working then we can use name attribute.

For displaying meta box values your code must be in loop.

$meta = get_post_meta($post->ID,'meta-box-text', true);

Here meta-box-text is name attribute of my input text field.

It works perfect for me.

| improve this answer | |
  • Why? Why do you think that it has to be in the loop? The first param for this function is post_id so you CAN use it anywhere, not only in the loop. Sorry, but your answer is just wrong. – Krzysiek Dróżdż Jun 16 '18 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.