問題描述

我需要在後期編輯儀錶板 metabox 與發佈作者 e-mail(或其他用户元字段) 。所以當管理員評論這篇文章時,可以進行編輯。

$meta_id = get_the_author_meta( 'user_email', $user_id );

$meta_box = array(
    'id' => 'my-meta-box',
    'title' => 'DANE FIRMY',
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'E-mail box',
            'id' => 'mail',
            'type' => 'text',
            'std' => $meta_id
        )
    )
);

當 $ user_id 是一個整數 (當我手動放在那裏為例 4)) 但是我想動態獲取當前作者 ID($user_id) 時,此代碼工作。

get_the_author_meta('user_mail')應該沒有指定 $user_id(codex 説:)),但是代碼是在 functions.php 和循環之外,所以它不工作。我從 Wordpress 和 PHP 開始,所以我不知道下一步該怎麼做。

也試過這個:

global $post;
$user_id=$post->post_author;

最佳解決方案

最簡單的方法是使用 get_post_field()

$post_author = get_post_field( 'post_author', $post_id );

有關這個問題的更多細節:看看 this StackOverflow answer

次佳解決方案

您可以使用以下內容:

/**
 * Gets the author of the specified post. Can also be used inside the loop
 * to get the ID of the author of the current post, by not passing a post ID.
 * Outside the loop you must pass a post ID.
 *
 * @param int $post_id ID of post
 * @return int ID of post author
*/
function wpse119881_get_author( $post_id = 0 ){
     $post = get_post( $post_id );
     return $post->post_author;
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。