CP zww大神很古老的WordPress评论、文章代码

  • A+
所属分类:主题插件

整理了一下zww大神博客CP的WordPress代码,虽然都是老代码,但是对于一些想要修改免费简单主题的童鞋还是很有帮助。

1、最新评论代码 - 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;}

这是之后z大神根据willin版本修改过的。

(willin的加了头像缓存后的代码,我这的是没有加缓存的)

<h3>Recent Comments</h3>
<ul class="recentcomments">
<?php //2010/4/25 更新 by willin
$limit_num = '8'; //这里定义显示的评论数量
$my_email = "'" . get_bloginfo ('admin_email') . "'"; //这里是自动检测博主的邮件,实现博主的评论不显示
$rc_comms = $wpdb->get_results("
 SELECT ID, post_title, comment_ID, comment_author, comment_author_email, comment_content
 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 comment_author_email != $my_email
 ORDER BY comment_date_gmt
 DESC LIMIT $limit_num
 ");
$rc_comments = '';
foreach ($rc_comms as $rc_comm) { //get_avatar($rc_comm,$size='50')
$rc_comments .= "<li>". get_avatar($rc_comm,$size='50') ."<span class='zsnos_comment_author'>" . $rc_comm->comment_author . ": </span><a href='"
. get_permalink($rc_comm->ID) . "#comment-" . $rc_comm->comment_ID
//. htmlspecialchars(get_comment_link( $rc_comm->comment_ID, array('type' => 'comment'))) // 可取代上一行, 会显示评论分页ID, 但较耗资源
. "' title='on " . $rc_comm->post_title . "'>" . strip_tags($rc_comm->comment_content)
. "</a></li>\n";
}
$rc_comments = convert_smilies($rc_comments);
echo $rc_comments;
?>
</ul>

使用:直接贴到sidebar.php即可使用。

由于这次willin的没有加截断函数,所以要靠css的overflow:hidden隐藏。参考我这个主题写的css

#sidebar .recentcomments img.avatar{width:16px;height:16px;float:left;position:relative;border:1px solid #ddd;margin:0 5px 0 0;padding:1px;}
#sidebar ul.recentcomments{list-style:none;padding-left:0;}
#sidebar ul.recentcomments li{margin:5px 0 0;line-height:20px;height:20px;overflow:hidden;}

2、最新文章代码 - 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%20the_permalink();%20?>"><?php the_title(); ?></a></span>
     <span><?php the_time('Y/m/d'); ?>.</span>
     </li>
     <?php endforeach; ?>
   </ul>

说明:numberposts 是文章数量

3、随机文章代码 - 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%20the_permalink();%20?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
   </ul>

4、最热文章代码 - 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%20echo%20get_permalink($postid);%20?>" title="<?php echo $title ?>">
        <?php echo $title ?></a> (<?php echo $commentcount ?>)</li>
        <?php } } ?>
   </ul>
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: