metabox - How to get meta box data to display on a page

admin2025-06-05  0

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

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

Share Improve this question edited Jun 16, 2018 at 8:33 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Mar 5, 2013 at 10:43 SyrehnSyrehn 3122 gold badges3 silver badges11 bronze badges 1
  • Any progress on that question? – kaiser Commented Oct 22, 2013 at 10:30
Add a comment  | 

4 Answers 4

Reset to default 8

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 ) );

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";
} 
$m_meta_description = get_post_meta($post->ID, 'images_url',true);

echo 'meta box value: ' . $m_meta_description;

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749128034a316602.html

最新回复(0)