如何让WordPress评论中的链接在新窗口打开

WordPress中很少使用target=_blank元素,也就是在新窗口打开,这样就容易损失流量,像评论中的链接如果能在新窗口打开就会还很多。那我们怎么实现在新窗口打开评论中的链接链接呢,小菜在网上找了下,有两种方法,可以修改代码来实现,也可以用插件来实现,因为装插件会影响WordPress的速度,而且修改代码很简单,所以插件法就不说了。下面是修改代码的方法:
打开wp-includes目录下comment-template.php文件。查找”function get_comment_author_link”找到以下这样一段代码:
function get_comment_author_link( $comment_ID = 0 ) {
/** @todo Only call these functions when they are needed. Include in if... else blocks */
$url = get_comment_author_url( $comment_ID );
$author = get_comment_author( $comment_ID );
if ( empty( $url ) || 'http://' == $url )
$return = $author;
else
$return = "<a href='$url' rel='external nofollow' class='url' >$author</a>";
return apply_filters('get_comment_author_link', $return);
}

因版本不同代码可能会有些差异,小菜用的是3.42.
将其中

<a href=’$url’ rel=’external nofollow’>$author</a>

添加target_blank更改成

<a href=’$url’ rel=’external nofollow’ target=’_blank’>$author</a>保存,就ok了。

  注意:修改代码前最好将comment-template.php文件备份,改错后还能覆盖恢复。
  不过用代码修改就是有一个问题,就是升级WordPress后要重新修改代码。
这是如何让WordPress评论中的链接在新窗口打开,如果想要评论者的链接也在新窗口打开请看《如何让WordPress中评论者的链接在新窗口中打开


关于小菜

有认识我的友友吗?

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注