wordpress评论者链接在新窗口打开

W3C标准认为链接新窗口打开属于强迫用户的行为,是不符合标准的,所以致wordpress3.0版本以后程序中所有链接均在本窗口打开,但是这样做,网站的pv会减少,跳出率会增高。特别是网站中最不应该跳出的评论链接,那么怎么设置使wordpress的评论者链接在新窗口打开呢?这里可以通过以下几种方式解决(因为不同网站的评论代码设置是不一样的)。

第一种方法:
1、在wordpress源程序wp-includes/comment-template.php中找到类似以下代码:

  1. function get_comment_author_link( $comment_ID = 0 ) {   
  2.     /** @todo Only call these functions when they are needed. Include in if… else blocks */  
  3.     $url    = get_comment_author_url( $comment_ID );   
  4.     $author = get_comment_author( $comment_ID );   
  5.   
  6.     if ( emptyempty$url ) || ‘http://’ == $url )   
  7.         $return = $author;   
  8.     else  
  9.         $return = “<a href=’$url’ rel=’external nofollow’ class=’url’>$author</a>”;   
  10.     return apply_filters(‘get_comment_author_link’, $return);   
  11. }  

2、在其中的一段代码:

  1. <a href=’$url‘ rel=’external nofollow’ class=’url’>$author</a>  

为以下代码:

  1. <a href=’$url‘ target=’_blank’ rel=’external nofollow’ class=’url’>$author</a>  

3、修改完成保存上传覆盖即可。

很遗憾,第一种方法我没有成功,因为我的评论文件使用的不是wordpress默认的评论文件,如果你也没有达到效果,请参照第二种方法解决:
第二种方法:
1、在wordpress源程序中wp-includes\formatting.php找到搜索以下代码:

  1. // Include parentheses in the URL only if paired   
  2. while ( substr_count( $url, ‘(‘ ) < substr_count( $url, ‘)’ ) ) {   
  3.     $suffix = strrchr$url, ‘)’ ) . $suffix;   
  4.     $url = substr$url, 0, strrpos$url, ‘)’ ) );   
  5. }   
  6.   
  7. $url = esc_url($url);   
  8. if ( emptyempty($url) )   
  9.     return $matches[0];   
  10.   
  11. return $matches[1] . “<a href=\”$url\” rel=\”nofollow\” target=\”_blank\”>$url</a>” . $suffix;   
  12.   

2、修改其中的链接代码:

  1. <a href=\“$url\” rel=\”nofollow\”>$url</a>  

为以下代码:

  1. <a href=\“$url\” rel=\”nofollow\” target=\”_blank\”>$url</a>  

3、保存上传覆盖。

真的想骂娘,折腾了半天竟然还是没有效果,原来想让自己的评论模块和别人不一样,付出的代价也是比别人大的,不过wordpress的魅力就在于此,如果你没有把自己折腾吐过,你根本不会明白成功后的那份喜悦(好像很像受虐狂 🙂 ),闲话少说,继续折腾。
第三种方法:
如果以上两个位置修改后依然没有效果,那就只能在一个位置了,当前主题的函数文件functions.php文件了,经过查找,确实在函数文件中存在以下一段代码:

  1. <a id=“commentauthor-<?php comment_ID() ?>” href=“<?php comment_author_url() ?>” rel=“external nofollow”>  

在a标签代码中添加上新窗口打开属性target=”_blank”即可,修改后的代码如下:

  1. <a target=“_blank” id=“commentauthor-<?php comment_ID() ?>” href=“<?php comment_author_url() ?>” rel=“external nofollow”>  

over!

周红川

分享独立站建站、搜索引擎优化,独立站推广相关实操经验和心得,如果你在跨境业务中遇到问题,欢迎加我微信沟通交流!

微信号:512765456

7 评论

  1. 小川老师,我想让评论不在当页显示,别人发表评论后全部到一个页面里面,在首页调用最新评论,该如何做呀,研究了好久,都没有成功?

留下一个评论