当前位置:首页WP教程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样式吧!

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

WordPress响应式图片解决方案

2023-3-8 22:09:22

WP教程

站外调用WordPress最新文章列表

2023-3-8 22:09:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
购物车
优惠劵
有新私信 私信列表
搜索