回复某位评论者的留言时,在留言前面加个 @评论者,这样可以更好地分辨是给谁回复。

也许网上已经有相关的教程,但是这里也分享一下我的实现方法,在当前主题functions.php中添加以下代码:

function mobantu_comment_add_at( $commentdata ) {    if( $commentdata['comment_parent'] > 0) {      $commentdata['comment_content'] = '@<a href="#comment-' . $commentdata['comment_parent'] . '">'.get_comment_author( $commentdata['comment_parent'] ) . '</a> ' . $commentdata['comment_content'];    }      return $commentdata; }  add_action( 'preprocess_comment' , 'mobantu_comment_add_at', 20);  

以上代码会直接将 @ 信息写入数据库,如果你不想将 @评论者 写入数据库,可以使用下面的代码:

function mobantu_comment_add_at( $comment_text, $comment = '') {    if( $comment->comment_parent > 0) {      $comment_text = '@<a href="#comment-' . $comment->comment_parent . '">'.get_comment_author( $comment->comment_parent ) . '</a> ' . $comment_text;    }      return $comment_text; }  add_filter( 'comment_text' , 'mobantu_comment_add_at', 20, 2);
www.npspro.top互联侠客
软师傅 » WordPress回复评论自动添加@评论者

最TOP的Docker,软路由,虚拟机等学习资料

立即查看 了解详情