一、最新评论代码 - Recent Comments
--------------------------
这个来自哪里我竟然忘了,那么就是来自互联网了,呵呵。这个是支持显示 gravatar 头像的,效果可以看我博客首页侧边栏。代码如下,使用 sql 实现的,对中文支持非常好,但英文博客就还是算了。
<h2>Recent Comments</h2>
<ul>
<?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,comment_author_email, SUBSTRING(comment_content,1,16) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND user_id='0' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {$output .= "\n<li>".get_avatar(get_comment_author_email('comment_author_email'), 24).strip_tags($comment->comment_author).":<br />" . " <a href=\"" . get_permalink($comment->ID) ."#comment-" . $comment->comment_ID . "\" title=\"on " .$comment->post_title . "\">" . strip_tags($comment->com_excerpt)."</a>...</li>";}
$output .= $post_HTML;
echo $output;?>
</ul>
说明:comment_content,1,16 中的16是每个留言的文字摘取数量;……ORDER BY comment_date_gmt DESC LIMIT 10 中的10是留言数量
最好用 css 自定义一下 gravatar 图片位置,可以参考一下我的:
#sidebar img.avatar{float:left;position:relative;border:1px solid #ddd;padding:1px;margin-right:5px;}
二、最新文章代码 - Recent Posts
这代码应该是来自
帕兰映像的了,可以直接去老帕那看,他那好东西很多,嘿嘿。代码如下:
<h2>Recent Posts</h2>
<ul>
<?php
$myposts = get_posts('numberposts=10&offset=0&category=0');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><span><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span>
<span><?php the_time('Y/m/d'); ?>.</span>
</li>
<?php endforeach; ?>
</ul>
说明:numberposts 是文章数量
三、随机文章代码 - Random Posts
同上也是来自
帕兰映像
<h3>Random Posts</h3>
<ul>
<?php
$rand_posts = get_posts('numberposts=10&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<!--下面是你想自定义的Loop-->
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
四、最热文章代码 - Hot Posts
忘了哪里搜来的,那么又是来自互联网!很好用,代码如下:
<h2>Hot Posts</h2>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> (<?php echo $commentcount ?>)</li>
<?php } } ?>
</ul>
五、本月评论最多的朋友 / 读者墙
<?php //本月评论最多的朋友
$identity="comment_author";
$passwordpost = " AND post_password=''";
$userexclude = " AND user_id='0'";
$approved = " AND comment_approved='1'";
$interval = 30;
$shownumber = 12;
$counts = $wpdb->get_results("SELECT COUNT(" . $identity . ") AS cnt, comment_author, comment_author_url,comment_author_email
FROM (SELECT * FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts
ON ($wpdb->posts.ID=$wpdb->comments.comment_post_ID)
WHERE MONTH(comment_date)=MONTH(now()) and YEAR(comment_date)=YEAR(now())" .
$userexclude . $passwordpost . $approved . ") AS tempcmt
GROUP BY " . $identity . " ORDER BY cnt DESC LIMIT " . $shownumber); ?>
<h2>本月最活跃的朋友</h2>
<ul>
<?php if ( $counts ) : foreach ($counts as $count) :
echo '<li>' . '<a href="'. $count->comment_author_url .
'" title="' . $count->comment_author . ' ('. $count->cnt . '评论)">' .get_avatar($count->comment_author_email,40).'</a></li>';
endforeach; endif;
?>
</ul>
说明:$shownumber 是显示的评论作者数量,因为每周的算法不准确,所以其他参数就不说明了。
**************************************************