【问题标题】:wordpress - retrieve content of post and comment in functions.phpwordpress - 在functions.php中检索帖子和评论的内容
【发布时间】:2011-04-22 23:39:42
【问题描述】:

我在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() 的变体,但没有成功。

【问题讨论】:

    标签: php wordpress comments


    【解决方案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 函数,因为每当我需要帖子中的一条信息时,我似乎最终还需要另一条信息。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      • 2021-07-13
      相关资源
      最近更新 更多