海天无影Blog

制作和海天博客一样的排行榜

/ 1368阅 / 55评 / 0

虽然开博以来,海天的博客才出现了两位风云人物,但是海天的排行榜还是在每个月更新着,就算哪一个月忘了,经过随缘同学的提醒也会立即补上的。看到有朋友需要海天博客的排行榜调用代码,正好今天没东西写,嘿嘿就写下吧。

其实这个代码不是海天原创,因为网上的wordpress类似的代码很多,海天也只是放在一个页面里,就成了海天博客的排行榜了。

其实这段生成代码,是参照的万戈同学的代码。代码只调用当月的排行榜,然后海天再将上个月的排行榜截图下来,发表上去就OK了。

排行榜的调用代码,只需要将下列代码放入functions.php文件中

$identity="comment_author";
$passwordpost = " AND post_password=''";
$userexclude = " AND comment_author != '海天无影'"; //这个是去掉名为海天无影的用户头像
$approved = " AND comment_approved='1'";
$shownumber = 24;
$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);

//real number of comments
function no_admin_number($no='', $one='', $twomore='') {
global $wpdb, $tablecomments, $post;
$comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND comment_author != '海天无影'");
$cnt = count($comments);
if (!$cnt)
echo $no;
elseif ($cnt == 1)
echo $one;
else
echo str_replace("%", $cnt, $twomore);
}

如果某个月你忘记截取图片了,只需要将其中的红色代码改为那个月份的数字,排行榜生成的就是那个月的排行了。

下面是制作排行榜页面了,新建文件listpage.php然后将下列代码复制进去,上传到根目录。

<?php
/*
Template Name: List Page Template PAGE
*/
?>
<?php get_header(); ?>
<div class="w970">
<div id="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<p>排行的描述,你自己写吧,呵呵~</p>
<br />
<div class="listwall">
<ul class="ffox_most_active">
<?php if ( $counts ) : foreach ($counts as $count) :
echo '<li>' . '<div class="ffox_most_active_img"><a href="'. $count->comment_author_url . '" target="_blank" title="' . $count->comment_author . ' ('. $count->cnt . '层楼)"><img src="/wp-content/plugins/mini-gravatar-cache/iava/'.md5(strtolower($count->comment_author_email)).'.jpg" alt="' . $count->comment_author . ' ('. $count->cnt . '层楼)" width="48px" height="48px"/></a><p>' . $count->comment_author . '</p></div></li>';
endforeach; endif;
?>
</ul>
</div>
<div class="content">
<?php the_content(); ?>
</div>
</div>
<div id="myads" class="sy-ad"></div>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<div class="post">哦!您要找的日志可能已经更换地址,重新搜索一下吧,或者点击<a title="Home" class="active" href="<?php echo get_option('home'); ?>/">这里</a>回首页看看吧</div>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>

然后去后台新建一个页面,模板选择你新建的文件listpage.php。这样以后发上个月排行榜的图的时候,只要直接在后台编辑页面,就可以了。

对了还有下面的css代码,复制到你的主题的style.css文件中,根据自己主题的需要更改细节:

.listwall {
width:620px;}
.ffox_most_active li {
float:left;
height:60px;
list-style:none outside none;
margin-left:5px;
overflow:hidden;
padding-bottom:20px;
width:70px;
}
.ffox_most_active_img{
height:48px;
text-align:center;
width:65px;
}
.ffox_most_active p {
text-align:center;
font-size:13px;
width:70px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注