wordpress - 在functions.php中检索帖子和评论的内容

发布于 2024-11-03 02:59:08 字数 378 浏览 2 评论 0原文

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

I have the method below in functions.php. I'm trying to get at the title of the post that the comment is being left on, and also the name, email, and content of the comment itself.

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)?
}

I've tried variations of the_title() and get_the_title(), but no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野の 2024-11-10 02:59:08

尝试一下:

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

希望这有帮助!

Give this a try:

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;
}

By using get_comment, you'll have access to all of this information about the comment: http://codex.wordpress.org/Function_Reference/get_comment#Return. Additionally, when you use get_post, you'll have access to all of this information: http://codex.wordpress.org/Function_Reference/get_post#Return.

Alternatively, you could simply use:

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

but I prefer to use the get_post function because whenever I need one piece of information from the post, I seem to eventually need another piece.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文