12
WordPess调用最新评论插件
想放个最新评论在首页显示,结果找了半天也没找到wordpress的调用最新评论的插件,很是郁闷~~~
算了,自己写个吧,这个是wordpress的调用最新评论的插件,大家可以看看~~
使用WordPess调用最新评论插件方法如下:
<h3>最新评论</h3>
<ul>
< ?php get_recent_comments(); ?>
</ul>
</div>
在使用< ?php get_recent_comments(); ?>的页面(比如我就是在sidebar.php中加入的)加入下面代码,即可使用本评论插件啦~~
function get_recent_comments($no_comments = 8, $before = ‘<li> ‘, $after = ”) {
global $wpdb, $tablecomments, $tableposts;
$request = “SELECT ID, post_title, comment_ID, comment_content, comment_author FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND (post_status = ‘publish’ OR post_status = ’static’) AND comment_author != ‘kimi’”;
$request .= “AND comment_approved = ‘1′ ORDER BY $tablecomments.comment_date DESC LIMIT $no_comments”;
$comments = $wpdb->get_results($request);
$output = ”;
foreach ($comments as $comment) {
$post_title = stripslashes($comment->post_title);
$comment_author = stripslashes($comment->comment_author);
$comment_content = strip_tags($comment->comment_content);
$comment_content = stripslashes($comment_content);
$comment_excerpt =cutstr($comment_content,30);
$permalink = get_permalink($comment->ID).”#comment-”.$comment->comment_ID;
$output .= $before . ‘<a href=”‘ . $permalink . ‘” title=”Comment By ‘ . $comment_author . ‘”>’ . $comment_excerpt . ‘</a>’ . $after;
}
echo $output;
}
function cutstr($string, $length) {
if(strlen($string) < = $length) {
return $string;
}
$string = str_replace(array(’&’, ‘"’, ‘<’, ‘>’), array(’&’, ‘”‘, ‘<’, ‘>’), $string);
$strcut = ”;
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t < 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
$strcut = str_replace(array(’&’, ‘”‘, ‘< ‘, ‘>’), array(’&’, ‘"’, ‘<’, ‘>’), $strcut);
return $strcut;
}
?>

这篇是测试WP的文章