如何在 wordpress 中以单个 post 显示自定义元框值?

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

我正在尝试制作自定义元框并在单个 post 中显示它的值,但代码不在单个 post.The 中显示值 值存储在 post 在管理员端,我想将该值显示为 well.Right 的前端端,现在我正在使用二十十七 theme.i 我在 function.php 文件中将此代码用于自定义元框:

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 . '"/>';
    }
}

我在 single.php 中尝试的代码如下所示:

               <?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 . '"/>';
                    }
                }
                ?>

尝试在您的 "template-parts/post/content" 文件中插入以下代码

    <?php
        $m_meta_description = get_post_meta($post->ID, 'm_meta_description', true);
        echo 'meta box value: ' . $m_meta_description;
    ?>
$meta = get_post_meta($post->ID,'meta-box-text', true);
                if($meta != '') {
                echo $meta;
                }else { 
                echo "Can't Display The Content";
                }

使用此代码。 这里的meta-box-text是name属性。 不要忘记在循环中插入这段代码

另一种方式:

$meta_print_value=get_post_meta(get_the_ID(),'meta-box-text',true);
echo($meta_print_value);