首页 > Wordpress心得 > 在新窗口打开Wordpress评论链接的方法

在新窗口打开Wordpress评论链接的方法

2010年4月16日 AEROFISH 2,131 views 发表评论 阅读评论

Wordpress默认评论链接是在同一窗口中打开,但有人可能希望在新窗口中打开评论链接。这需要分两种情况来处理。

第一种情况是主题调用的是默认的WordPress评论函数,那么就去wp-includes\comment-template.php文件中修改。

找到下面这段:

function get_comment_author_link() {
	/** @todo Only call these functions when they are needed. Include in if... else blocks */
	$url    = get_comment_author_url();
	$author = get_comment_author();

	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);
}

在对应的a标记中添加target='_blank'属性,然后保存上传即可。

第二种情况是主题使用了自己定义的评论函数,那就只能修改主题的评论模版了。每个主题处理评论的方式都会不同,但大多会调用WordPress底层的comment_author_url()这个函数,可以搜索它,然后再添加target='_blank'属性。以主题inove1.4.6版本为例,在inove\functions.php中,第546行修改。

2010年8月19日补充另一解决方法:

由于评论中的链接基本都是外部链接,因此可以让网站自动识别外链,然后自动加上target='_blank'属性。方法采用jQuery技术。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
<!--
$("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").addClass("external").attr("target","_blank");
-->
</script>
【最后修改时间:2010年11月5日】

本文对我无帮助,减1分本文对我有帮助,加1分 (本文对您有帮助吗?)
Loading ... Loading ...

  1. 2010年7月26日12:06 | #1

    不错,我已经按你的教程搞定了,谢谢了

    [嵌套回复]

  2. 2010年9月1日21:39 | #2

    谢谢分享

    [嵌套回复]

  3. 2010年10月11日20:20 | #3

    轻松实现,感谢~~

    [嵌套回复]

  4. 2011年3月1日00:55 | #4

    看了很多个博客的方法,只有博主的全面,已经解决了,谢了

    [嵌套回复]

  1. 本文目前尚无任何 trackbacks 和 pingbacks.