首页 » WordPress教程 » 正文

如何让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中评论者的链接在新窗口中打开

本文共 15 个回复

  • mariaescort-kiev.com 2020/05/06 07:06

    I believe everything published was actually very reasonable. But, consider this, suppose you added a little content? I am not saying your content isn't solid., however what if you added something to maybe get people's attention? I mean 如何让WordPress评论中的链接在新窗口打开 | 手机小菜的博客 is a little boring. You might peek at Yahoo's front page and watch how they create news headlines to grab people to click. You might add a video or a related pic or two to get readers excited about what you've got to say. In my opinion, it could bring your posts a little bit more interesting.

  • giup viec 2020/01/05 08:47

    Thanks for finally talking about >如何让WordPress评论中的链接在新窗口打开 | 手机小菜的博客 <Loved it!

发表评论