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

10,641

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;
    ?>
Share:
10,641

Related videos on Youtube

Admin
Author by

Admin

Updated on May 25, 2022

Comments

  • Admin
    Admin over 1 year

    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 . '"/>';
                        }
                    }
                    ?>
    
    • Vel
      Vel about 6 years
      your code working fine in twenty seventeen theme.
    • Admin
      Admin about 6 years
      In my case in single post,there is not displaying custom meta box value...
    • Vel
      Vel about 6 years
      did you update the value in single post?
    • Admin
      Admin about 6 years
      No, i am using code at before end of endwhile loop,but it display only main post content,not custom meta box content,while custom meta box content is already stored at admin side.
    • Vel
      Vel about 6 years
      its working fine me...
    • Admin
      Admin about 6 years
      I have included 2 screenshots on top of the question.
  • Admin
    Admin about 6 years
    Thanks, works perfect, i forget to modify content file.