当前位置:首页教程中心网站教程WordPress添加评论引用方法

WordPress添加评论引用方法

嵌套评论可以增加用户的互动,通常将嵌套评论显示在父评论下面,每一层嵌套都缩进一点距离,当嵌套层数多了之后问题就来了,后面的嵌套显示会变得非常短,导致整个界面类似倒立的金字塔,非常难看。

所以我们需要类似论坛的评论引用方式,当用户回复其他用户的评论,该用户的评论不会位于嵌套里,但会引用父评论的内容。

把以下代码丢到主题文件functions.php中:

function bzg_add_comment_text( $comment_content, $comment ) {$post_id = $comment->comment_post_ID;$reply_link = \'<div ><a rel=\"nofollow\"  href=\"\' . get_permalink( $post_id ) . \'?replytocom=\' . $comment->comment_ID . \'#respond\" onclick=\"return addComment.moveForm( \\\'div-comment-\' . $comment->comment_ID . \'\\\', \\\'\' . $comment->comment_ID . \'\\\', \\\'respond\\\', \\\'\' . $post_id . \'\\\' )\" aria-label=\"回复给\' . $comment->comment_author . \'\">回复</a></div>\';$parent_id = $comment->comment_parent;if( ! $parent_id ) {return $comment_content . $reply_link;}$add_text = \'<blockquote ><p >\' . get_comment_author( $parent_id ) . \' 发表于 \' . get_comment_date( \'Y-m-d H:i:s\', $parent_id ) . \'</p><p >\' . get_comment_excerpt( $parent_id ) . \'</p></blockquote>\';return $add_text . $comment_content . $reply_link;}if( ! is_admin() ) {add_filter( \'get_comment_text\', \'bzg_add_comment_text\', 10, 2 );}

这样,每一条评论都允许其他用户回复,并且在回复的评论内容中引用了父级评论摘要,网上现有的实现原理是用JS来获取父评论内容并插入到新评论中,然后保存到数据库,这样有后遗症,本文的方法不会影响数据库,只在页面里输出。

此时可能会发现回复的评论与父评论扎堆出现了,并没有按评论的时间排序,找到主题文件comments.php,如果你的评论内容是用wp_list_comments()函数来输出的,可以这样写:

$comments = get_comments( array(\'orderby\' => \'comment_date\',\'order\' => \'ASC\',\'type\' => \'comment\',) );wp_list_comments( array(\'style\' => \'ol\',\'max_depth\' => -1,\'avatar_size\' => 36,\'format\' => \'html5\',\'type\' => \'comment\',\'reverse_top_level\' => false,), $comments );

大功告成,赶紧去添加CSS样式吧!

温馨提示:

文章标题:WordPress添加评论引用方法

文章链接:https://www.muooy.cn/2226.html

更新时间:2025年07月02日

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:305582964@qq.com,我们将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读用户协议免责声明

给TA打赏
共{{data.count}}人
人已打赏
网站教程

WordPress 响应式图片优化全方案:从基础实现到深度优化

2025-6-29 21:47:05

网站教程

站外调用 WordPress 最新文章列表的实现方案

2025-6-30 12:40:01

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
购物车
优惠劵
今日签到
搜索