2013-06-28 43 views
0

我創建一個函數,當我的WordPress博客上發佈帖子時,發送電子郵件到郵件列表。WordPress的:不要設法從php函數檢索作者的名字

function announce_post($post_id){ 
    $email_address = '[email protected]'; 

    $subject = "New Post: " . get_the_title($post_id); 
    $body = "Hi,\r\n\r\n" . 
     "SOMEONE has just published the article \"" . 
     get_the_title($post_id) . "\" on \"BLOG TITLE\".\r\n\r\n" .  
     "You can read it at " . get_permalink($post_id) . "\r\n" . 
     "or visit BLOG_ADDRESS.\r\n\r\n" . 
     "Best wishes\r\n" . 
     "The Publisher"; 

    if (wp_mail($email_address, $subject, $body, "From: \"BLOG TITLE\" <[email protected]>")) { } 
} 

add_action('publish_post','announce_post'); 

,因爲它是功能效果很好,但我當然會與實際職位的作者姓名替換SOMEONE。我無法找回那個。
get_the_author($post_id)get_post_meta($post_id, 'author_name', true)也沒有其他的我試過,不記得工作。一切都剛剛返回""

那麼,在給定帖子ID的情況下,檢索帖子作者姓名的正確方法是什麼?

+0

檢查語法高亮,看到了嗎?這是你的代碼中的一個問題,你會得到語法錯誤嗎? – elclanrs

+0

不,它發生在用假人替換博客標題時。 –

+0

該功能在服務器上工作,現在看起來也不錯... –

回答

1

get_the_author()是一個(也許是誤導性)功能,旨在用於。這只是參數is now deprecated。另外值得一提的是,作者數據不會作爲後期元存儲,因此任何嘗試都將徒勞無功。

你實際上應該使用get_the_author_meta('display_name', $author_id)。我會建議接受第二個參數在你的鉤子,這是$post對象,以獲取作者ID:

function announce_post($post_id, $post) { 
    $name = get_the_author_meta('display_name', $post->post_author); 
} 

add_action('publish_post','announce_post', 10, 2); 
+0

謝謝。我想我自己從來沒有通過文檔發現過這個問題:-( –