前面我们讲了如何实现在wordpress中完成评论链接重定向,至于为什么要评论链接重定向以及如何实现评论链接重定向请看这篇文章让你的wordpress评论链接重定向,这样一来我利用jQuery 在新窗口打开外部链接的方法就失效了,因为评论链接重定向实现的链接结构是自已的域名/?=访客的域名.这样以来点击评论者的链接就很可能就此丢失了一些PV,这绝对不是小问题,OK,终于在万戈哪里找到了方法,以此备忘以后好找呵呵!
第一个方法就是改源码,打开/wp-includes/comment-template.php,查找“function get_comment_author_link”我用的是wordpress3.1.3版本,目前的最新版,在147行至157行.原代码是
1
2
3
4
5
6
7
8
9
10
11 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);
}改为
1
2
3
4
5
6
7
8
9
10
11 ffunction 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' ' target='_blank' rel='external nofollow' class='url'>$author</a>";
return apply_filters('get_comment_author_link', $return);
}这方法不用说了,哪就是升级wordpress后又要弄了,搞不好,到时又忘记在哪里改了.
第二个方法就是在主题里加载一个JS使之评论链接在新窗口打开.以下是JS代码
1
2
3
4
5
6
7
8
9 function externallinks(){
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++){
var anchor = anchors[i];
if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
window.onload = externallinks;复制到你以前加载过的JS里就可以了.如果你没有加载过JS你还得用以下语句调用一下.把上面的JS保存为external.js
1 <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/external.js"></script>以上二个方法你取一个就可以了.以上方法仅能实现评论链接从新窗口打开,若是第三方链接(也就是外部链接)也想在新窗口打开,你还得在你的已加载的JS中加入以下代码.
1
2
3
4
5 $(document).ready(function() {
$("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])")
.addClass("external")
.attr("target","_blank");
});
请问RSS小工具的链接用新窗口打开,能用JS调用吗
来了就留点什么再走吧!可以按Cfdsfsdtrl+Enter提交哦!
还有一个是用hook的方法.在主题中实现 不用害怕wp升级.
@iSayme 能改主题就最好了.源码上改升级就麻烦了
建议在主题中实现此功能,或者使用插件。改wp文件的话,每次升级都要改一次,很麻烦
先收藏一下,以后再慢慢研究,以后可能用得着。
这个方法以前用过,但是wordpress更新太频繁,所以现在就让他顺其自然了!
@闲云野鹤 是的呀,wordpress更新真可谓是姨妈级的