如何在Wordpress中的单个帖子中显示自定义元框值?

用户名

在此处输入图片说明在此处输入图片说明我试图制作自定义元框并在单个帖子中显示其值,但代码在单个帖子中不显示值。该值存储在管理员端的帖子中,我也希望将该值显示为前端。现在我正在使用二十七个主题。我正在使用此代码作为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;
    ?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在WordPress中获取具有特定元值的帖子的所有自定义术语?

wordpress:如何更改单个自定义帖子类型的元显示

如何在WordPress响应中显示自定义帖子类型的JSON响应中的自定义字段?

无法在自定义元框 textarea wordpress 中显示值

如何在WordPress的自定义页面模板中显示帖子?

如何在Wordpress中显示最后三种自定义帖子类型

如何在Wordpress中对自定义帖子进行分类并分别显示

WordPress 中单个帖子的高级自定义字段单个值

如何在Wordpress中的视觉编辑器中显示自定义的简码单个图像?

如何在自定义帖子中添加 wordpress 自定义部分

自定义帖子类型“单个”不会在wordpress中显示我的代码

如何在wordpress中创建自定义元表?

如何在前端的视觉编辑器中显示新的自定义元框

在元框中循环自定义帖子类型

自定义元框的值在帖子中重复出现

如何在WordPress中自定义帖子类型的自定义类别的帖子循环?

如何在Wordpress中获取自定义帖子类型的最新帖子ID

在Wordpress中显示自定义帖子类型的内容

如何在Wordpress中以编程方式显示自定义帖子类型的图像缩略图?

如何停止在搜索结果中显示 WordPress 中的自定义帖子类型

如何在wordpress插件的自定义元框中保存多个复选框值?

如何在管理单个产品编辑页面中获取产品类别自定义元字段的值?

如何在wp Media js中获取自定义帖子元

在自定义主题 wordpress 的元框中编辑标题

Wordpress中的自定义链接和名称元框

如何修复WordPress中的“按类别显示自定义帖子”

WordPress:如何通过自定义分类法在作者页面中显示帖子数

如何将一种自定义帖子类型的所有帖子显示到另一种自定义帖子框中?

如何在 Wordpress 中为自定义帖子类型加载自定义样式表?