Dark的一些调整
昨天在Gtalk上有个同学问我如何修改Dark主题的侧边栏的评论部分。那个是柠檬同学自己写的一个函数,修改的话在fuctions.php中的recent_cmts函数,其实里面就一个函数。只是显示了评论时间,而没有具体的内容,如果需要显示评论内容的话,修改原来recent_cmts函数中的SQL就可以实现了。
突然想起上次在Nicky那里看到一个Gravatar插件的介绍,如果把这个用起来的话应该效果挺不错的。立刻下载了Gr Icon了,不知道是不是RPWT,没成功。
wp_gr_recent_comments函数的说明
前面4个参数和Gravatar的是一样的,分别是:显示等级、图标大小、默认图标、边框设置,
新加入一个参数:显示多少条留言,这是从中文工具箱拷贝过来的功能,详细信息请参考中文工具箱的get_recent_comments函数。
作者给了个例子:
< ?php wp_gr_recent_comments("X", "42", get_bloginfo('template_url')."/images/24_feet.png", "","10"); ?>
?>
我就是没看出边框设置应该怎样设?看作者说明说可以参考桑葚的中文WordPress工具箱。看了下说明,心里大概知道该怎么做了,可为了实现这么一点小的改动,多安装两个插件也太麻烦了。决定自己动手,刚好那本号称PHP圣经的《Beginning PHP and MySQL 5》也看了几十页了。
看来代码之后知道需要在css文件中增加一个get_gravatar的定义,不过还是不知道在函数中应该怎样添加。
下面看看我在fuctions.php 中增加的丑陋的代码吧。代码很乱,没整理,功能实现了再说。99%都是从上面两个插件中Copy出来的。如果有Bug的话,我只有1%^_^
global $wpdb;
$request = "SELECT ID, comment_ID, comment_author_email, comment_content, comment_author FROM $wpdb->posts, $wpdb->comments WHERE $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish' AND comment_author != 'gYtHialY'";
$request .= "AND comment_approved = '1' ORDER BY $wpdb->comments.comment_date DESC LIMIT
$no_comments";
$comments = $wpdb->get_results($request);
foreach ($comments as $comment) {
$comment_author_email = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($comment->comment_author_email);
$comment_author = stripslashes($comment->comment_author);
$comment_content = strip_tags($comment->comment_content);
$comment_content = stripslashes($comment_content);
$comment_excerpt =substr($comment_content,0,35);
$comment_excerpt = utf8_trims($comment_excerpt);
$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
if($rating && $rating != '')
$comment_author_email .= "&rating=".$rating;
if($size && $size != '')
$comment_author_email .="&size=".$size;
if($default && $default != '')
$comment_author_email .= "&default=".urlencode($default);
if($border && $border != '')
$comment_author_email .= "&border=".$border;
echo "<a href=\"$permalink\" title=\"$comment_author Says: $comment_excerpt\"><img src=\"$comment_author_email\" class=\"get_gravatar\" alt=\"$comment_author\" /></a>";
}
return;
}
function get_recent_comments_only($no_comments = 5, $before = '<li> ', $after = '</li>', $show_pass_post = false) {
global $wpdb, $tablecomments, $tableposts;
$request = "SELECT ID, comment_ID, comment_content, comment_author FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND (post_status = 'publish' AND comment_author != 'gYtHialY' OR post_status = 'static') AND comment_type = ''";
if(!$show_pass_post) { $request .= "AND post_password ='' "; }
$request .= "AND comment_approved = '1' ORDER BY $tablecomments.comment_date DESC LIMIT
$no_comments";
$comments = $wpdb->get_results($request);
$output = '';
foreach ($comments as $comment) {
$comment_author = stripslashes($comment->comment_author);
$comment_content = strip_tags($comment->comment_content);
$comment_content = stripslashes($comment_content);
$comment_excerpt =substr($comment_content,0,35);
$comment_excerpt = utf8_trims($comment_excerpt);
$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
$output .= $before . '<a href="' . $permalink . '" title="View the entire comment by ' . $comment_author . '">' . $comment_author . '</a>: ' . $comment_excerpt . '...' . $after;
}
echo $output;
}
function utf8_trims($str) {
$len = strlen($str);
for ($i=strlen($str)-1; $i>=0; $i-=1){
$hex .= ' '.ord($str[$i]);
$ch = ord($str[$i]);
if (($ch & 128)==0) return(substr($str,0,$i));
if (($ch & 192)==192) return(substr($str,0,$i));
}
return($str.$hex);
}
修改sidebar.php
用下面的代码
<h2>Recent Comments</h2>
< ?php wp_gr_recent_comments("X", "24", get_bloginfo('template_url')."/images/default_24.gif", "","16"); ?>
<div class="recent-cmts">
<ul id="recent-cmts">< ?php get_recent_comments_only(16,'<li class="recent-cmts">','',false);?></ul>
</div>
?>
替换
<h2>Recent Comments</h2>
<div class="recent-cmts">
<ul id="recent-cmts">< ?php recent_cmts('10'); ?></ul>
</div>
?>
最后修改style.css
增加get_gravatar的定义
background: #0b245b;
margin:1px;
border: 2px solid #0f2b6f;
}
修改下recent-cmts的定义,使之看起来紧凑一点
clear: both;
padding:8px 3%;
background: transparent url(images/separator_rct-cmt.gif) repeat-x left bottom;
line-height: 120%;
}
替换原来的
clear: both;
padding: 15px 3%;
background: transparent url(images/separator_rct-cmt.gif) repeat-x left bottom;
line-height: 140%;
}
Ok,大功告成了。效果见右侧我的评论。这里看吧,感觉还是原来的比较好看,改回去了,给需要的人一个参考。











看来Nicky明气真大…
我订阅了他的rss,所以知道~^_^