【问题标题】:WordPress user data not showing up in meta boxWordPress 用户数据未显示在元框中
【发布时间】:2018-07-22 05:19:36
【问题描述】:

我为自定义帖子类型创建了一个新的元框,但我无法让数据(用户所在的城市)显示在 Location 下。

这是我的代码:

function wporg_add_custom_box()
{
        add_meta_box(
            'wporg_box_id',           // Unique ID
            'Location',               // Box title
            'custom_meta_box_markup',  // Content callback, must be of type callable
            'project',                // Post Type
            'side', 
            'core');
}
add_action('add_meta_boxes', 'wporg_add_custom_box');

function custom_meta_box_markup() {
    global $post;
    $custom_fields = get_the_author_meta( 'city', $author_id );
    ?>
        <div>
            <input name="custom_fields" type="text" value="<?php echo $custom_fields;?>">
        </div>
<?php }

【问题讨论】:

    标签: wordpress metadata meta-boxes


    【解决方案1】:
    syntax: get_the_author_meta( string $field = '', int $user_id = false )
    

    Codex 说:返回:(字符串)当前作者的 DB 对象中的作者字段,否则为空字符串。

    我相信你的函数返回的是空结果。

    试试:

    get_the_author_meta( 'city', get_current_user_id() );
    

    如果您正在编辑,您可以使用$post-&gt;author_id 而不是get_current_user_id()

    【讨论】:

    • get_the_author_meta('city', get_current_user_id());将我的位置呈现为管理员,而不是用户/作者的位置 --- 我也尝试了 $post->author_id,但没有呈现任何数据
    • 您可以使用var_dump 调试get_the_author_meta() 的结果,同时检查您的用户作者是否输入了所有信息,可能城市字段为空。
    • 不知道怎么做var_dump...作者是我创建的一个假用户。我已经设法在网站的其他管理区域呈现用户位置/城市,只是无法在编辑帖子页面上获得它。
    • 因此,如果它在管理员中工作,它必须在 $post 作者中工作。尝试调试它,在global $post 之后立即执行var_dump($post),并查看正确的作者和/或帖子是否正在返回...另请参阅:stackoverflow.com/questions/14743342/…
    • var_dump 显示正确的作者 -- 16 是假用户的 ID ["post_author"]=> string(2) "16"
    【解决方案2】:

    解决方案:

    $custom_fields = get_the_author_meta('city', $author_id=$post->post_author);

    【讨论】:

      猜你喜欢
      • 2013-12-16
      • 2018-03-15
      • 2019-02-06
      • 2016-12-21
      • 2021-12-23
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 2019-08-17
      相关资源
      最近更新 更多