2011-04-22 45 views
1

我在function.php下面的方法。我正在嘗試查看評論留下的帖子的標題,以及評論本身的名稱,電子郵件和內容。wordpress - 檢索文章內容和評論在functions.php

add_action('comment_post', 'comment_posted'); 

function comment_posted($comment_id) { 
    //what can I do here to get the original title of the post 
    //what can I do here to get the details of the comment (name, email, content)? 
} 

我試過變化的the_title()和get_the_title(),但沒有運氣。

回答

1

試試這個:

add_action('comment_post', 'comment_posted'); 

function comment_posted($comment_id) 
{ 
    $comment = get_comment($comment_id); 
    $post = get_post($comment->comment_post_ID); 
    $title = $post->post_title; 
} 

使用get_comment,你將有機會獲得所有關於此評論的信息:http://codex.wordpress.org/Function_Reference/get_comment#Return。另外,當您使用get_post時,您將可以訪問所有這些信息:http://codex.wordpress.org/Function_Reference/get_post#Return

或者,你可以簡單地使用:

$comment = get_comment($comment_id); 
$title = get_the_title($comment->comment_post_ID); 

但我更喜歡,因爲每當我需要從後一個信息,我似乎最終需要另一塊使用get_post功能。

希望這會有所幫助!

+0

這太棒了!正是我在找什麼。謝謝。 – sol 2011-04-25 17:14:20