Display the Metadata is not showing anything in the front-end in cmb2

368 Views Asked by At

I am building a website using Wordpress in where I want to show the Metadata from the custom fields. I have setup the cmb2 in my function.php as like below the codes..

add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );

function cmb2_sample_metaboxes() {

    
    $cmb = new_cmb2_box( array(
        'id'            => 'test_metabox',
        'title'         => __( 'Test Metabox', 'cmb2' ),
        'object_types'  => array( 'page', ), 
        'context'       => 'normal',
        'priority'      => 'high',
        'show_names'    => true, 
        
    ) );

    
    $cmb->add_field( array(
        'name'       => __( 'Test Text', 'cmb2' ),
        'desc'       => __( 'field description (optional)', 'cmb2' ),
        'id'         => 'yourprefix_text',
        'type'       => 'text',
        'show_on_cb' => 'cmb2_hide_if_no_cats', 
        
        
        
    ) );

}

Ok, that is working in the post section, That's working. But when I tried to show Metadata in the front-end using

<?php

$text = get_post_meta( get_the_ID(), '_yourprefix_text', true );


echo esc_html( $text );
?>

nothing is echoing.

Anyone please find out what the problems are.

1

There are 1 best solutions below

2
nickless On

Looks like you've just got a typo. The id does not match the meta_key: 'yourprefix_text' vs '_yourprefix_text'

Fixed:

<?php

$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );


echo esc_html( $text );
?>