2025年4月16日 星期三 乙巳(蛇)年 正月十七 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > 万维网络 > WordPress

WordPress 中如何更改评论者链接为站内链接

时间:12-14来源:作者:点击数:18
城东书院 www.cdsy.xyz

WordPress 的每篇文章的评论,如果评论者填写了网址,那么在显示的时候就会加上这个连接,实际上是不利于优化的,分散了网站的权重,那么 WordPress 中如何更改评论者链接为站内链接。

WordPress 中如何更改评论者链接为站内链接

评论链接重定向

在 functions.php 文件加入以下代码

  • add_filter('get_comment_author_link', 'add_redirect_comment_link', 5);
  • add_filter('comment_text', 'add_redirect_comment_link', 99);
  • function add_redirect_comment_link($text = ''){
  • $text=str_replace('href="', 'href="'.get_option('home').'/?r=', $text);
  • $text=str_replace("href='", "href='".get_option('home')."/?r=", $text);
  • return $text;
  • }
  • add_action('init', 'redirect_comment_link');
  • function redirect_comment_link(){
  • $redirect = $_GET['r'];
  • if($redirect){
  • if(strpos($_SERVER['HTTP_REFERER'], get_option('home')) !== false){
  • header("Location: $redirect");
  • exit;
  • }else {
  • header("Location: http://www.cdsy.com.cn");
  • exit;
  • }
  • }
  • }

在网站根目录下的 robots.txt 文件中添加以下代码,用来屏蔽搜索引擎收录重定向后的网址

  • Disallow:/?r=*

再来看看前台评论者的链接吧,都变成如下形式了

  • http://www.cdsy.com.cn/?r=http://www.cdsy.com.cn

但如果你的博客使用的是多说插件,那以上所有写的东西都是废话,没办法对多说里的评论起作用。

评论连接加上 nofollow 功能

找到 WordPress 的 functions.php,然后在其中添加以下代码,保存上传即可。

  • //评论加Nofollow
  • function add_nofollow_to_comments_popup_link(){
  • return ' rel="nofollow" ';
  • }
  • add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link');

顺便优化以下,要加的地方有两个,一个是评论者的链接,还有就是那个 回复 这也是个链接。

  • //为评论者添加nofollow属性
  • function add_nofollow_to_comments_popup_link(){
  • return 'rel="nofollow" ';
  • }
  • add_filter('comments_popup_link_attributes', 'add_nofollow_to_comments_popup_link');
  • //为评论回复链接加nofollow属性
  • add_filter('comment_reply_link', 'add_nofollow_to_replay_link');
  • function add_nofollow_to_replay_link( $link ){
  • return str_replace( '")'>', '")' rel='nofollow'>', $link );
  • }
城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐