How to display custom meta box value in single post in wordpress?

user8443350 picture user8443350 · Aug 17, 2017 · Viewed 8.9k times · Source

enter image description hereenter image description hereI am trying to make custom meta box and display it's value in single post,but code doesn't display value in single post.The value is stored in post at admin side,i want to display that value as front end side as well.Right now i am using twenty seventeen theme.i am using this code for custom meta box in function.php file:

add_action( 'add_meta_boxes', 'm_param_meta_box_add' );
function m_param_meta_box_add() {
    add_meta_box( 'm_param_post', 'Box Title', 'm_param_post_meta_box_cb', 'post', 'normal', 'high' );
}

function m_param_post_meta_box_cb( $post )
{
    $values = get_post_custom( $post->ID );
    if ( isset( $values['m_meta_description'] ) ) {
        $m_meta_description_text = esc_attr( $values['m_meta_description'][0] );
    }
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="m_meta_description">Post Description</label></th>
<td><textarea rows="3" cols="50" name="m_meta_description"><?php echo $m_meta_description_text; ?></textarea></td>
</tr>
</table>

<?php
} // close m_param_post_meta_box_cb function

add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    // Make sure your data is set before trying to save it
    if( isset( $_POST['m_meta_description'] ) ) {
        update_post_meta( $post_id, 'm_meta_description', wp_kses( $_POST['m_meta_description']) );
    }    
}

add_action('wp_head', 'add_to_wp_head');
function add_to_wp_head( )
{
    if (is_single())
    {
        global $post;
        $m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
        echo '<meta name="description" content="' . $m_meta_description . '"/>';
    }
}

The code that i am trying in single.php is look like:

               <?php
                add_action('wp_head', 'add_to_wp_head');
                function add_to_wp_head( )
                {
                    if (is_single())
                    {
                        global $post;
                        $m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
                        echo '<meta name="description" content="' . $m_meta_description . '"/>';
                    }
                }
                ?>

Answer

Avi picture Avi · Aug 17, 2017

Try to insert below code in your "template-parts/post/content" file

    <?php
        $m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
        echo 'meta box value: ' . $m_meta_description;
    ?>