A-A+

WordPress去掉侧边栏评论用户的网址

本文于2012年05月18日最后更新 博客心得

昨天张自然给博客侧边栏加入了最新评论功能,可一看,这样就是给别人无偿做单向链接,严重影响网站排名,有可能还会被k站1

今天张自然分别说说以下两种不同情况下如何去掉评论用户的网站链接。

去掉代码生成的侧边栏最新评论网址方法

这种方式我就直接贴代码了,你就慢慢看,看不懂的可以给我留言。

 <li><h2>最新评论</h2>
 <?php  
  global $wpdb;  
  $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,   comment_post_ID, comment_author, comment_date_gmt, comment_approved,   comment_type,comment_author_url,   SUBSTRING(comment_content,1,30) AS com_excerpt   FROM $wpdb->comments   LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =   $wpdb->posts.ID)   WHERE comment_post_ID <>1289 AND comment_approved = '1' AND comment_type = '' AND   post_password = ''   ORDER BY comment_date_gmt DESC   LIMIT 8";  
  $comments = $wpdb->get_results($sql);  
  $output = $pre_HTML;  
  $output .= "\n<ul id=\"recentcomments\">";  
  foreach ($comments as $comment) { 
   $output .= "\n<li class=\"recentcomments\">". "<a href=\"" . $comment->comment_author_url . "\" target=_blank>" .strip_tags($comment->comment_author) ."</a>"   ." 说: " . "<a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . $comment->post_title . "\">" . "<font color=\"#006600\">". strip_tags($comment->com_excerpt) ."</font></a></li>";  
  }  
  $output .= "\n</ul>";  
  $output .= $post_HTML;  
  echo $output;
 ?>
 </li>

去掉小工具生成的侧边栏网址方法

很多WordPress主题支持小工具功能,里面包含了很多实用的功能模块,比如最新文章、最新评论、分类目录、日志归档等等,用户可以把这些小功能模块拖到右边的侧边栏区域,保存之后当我们访问前台页面的时候,侧边栏就会自动读取数据库的内容,予以显示。

上面的扯得有点远了,我们回到本文的主题上来。以上面这种模式生产的侧边栏,最新评论模块自动读取最新的用户评论,并且列表显示出来。

WordPress默认的会给用户名称加上超链接,指向用户填写评论留言时留下的网址。那这样该如何修改呢?

打开WordPress下面的 wp-includes 文件夹,找到default-widgets.php文件并打开,定位到下面的代码:

  $output .= '<ul id="recentcomments">';
  if ( $comments ) {
   foreach ( (array) $comments as $comment) {
    $output .=  '<li>' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
   }
  }

将上面代码改为下面代码,保存即可。

$output .= '<ul id="recentcomments">';
if ( $comments ) {
foreach ( (array) $comments as $comment) {
$output .= '<li>' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s :<br /> %2$s', 'widgets'), get_comment_author(), '<a target="_blank" href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . strip_tags( $comment->comment_content) . '</a>') . '</li>';
}
}

对比上面两段代码你会发现2个不同,一是我把 get_comment_author_link() 改成了 get_comment_author(),去掉了网址链接。 二是改变了显示样式和增加了换行和网址在新窗口打开!

  1. 等待思索
    直接加nofollow就行了!!!!

给张自然个人博客留言取消回复

Copyright © ZhangZiRan.com All Rights Reserved. 张自然个人博客 内容版权所有,同时保留所有权利。个人博客免责声明

用户登录