调用三个排行榜的实现

         感谢 集思 提供。http://www.giisi.com/design/wp-reader-wall-2.html

三个排行榜的实现无非就是新建一个单页面模板。

复制你当前模板中的page.php文件,从新命名为什么可以自己定,我这里是命名为:giisi-comment-central.php。

我编辑工具打开新文档giisi-comment-central.php,把下面的代码复制到文档的最上方:

<?php /* Template Name: Comments Central */ ?>

保存下这个文档。

好了,一个但页面的模板就做好了。下面就可以实现各个排行榜了。

最新评论

把下面的代码复制到新建的giisi-comment-central.php文档中:

<h2>Recent Comments</h2>

<ul>

<?php

$max = 7; // number item to get global $wpdb;

$sql = “SELECT c.*, p.post_title FROM

$wpdb->comments c INNER JOIN

$wpdb->posts p ON (c.comment_post_id=p.ID) WHERE comment_approved = ‘1’

AND comment_type not in (‘trackback’,’pingback’) ORDER BY comment_date DESC LIMIT $max”;

$results = $wpdb->get_results($sql);

$template = ‘%g <a href=”%au”>%an</a> on <a href=”%pu#comment-%cid”>%pt</a>’;

$echoed = 0; foreach ($results as $row)

{ $tags = array(‘%ct’,’%cd’,’%g’,’%pt’,’%pu’,’%au’,’%an’,’%cid’);

$replacements = array($row->comment_title,

$row->comment_date,get_avatar($row->comment_author_email,’32’),

$row->post_title, get_permalink($row->comment_post_ID),

$row->comment_author_url,

$row->comment_author,

$row->comment_ID); echo ‘<li>’ . str_replace($tags,$replacements,$template) . ‘</li>’; $echoed = 1;

}

if ($echoed==0)

echo ‘<li>No comment found.</li>’;

?>

</ul>

当然这样是没有样式的,因为,我们还没有添加相关的样式进入我们的外链样式表:

#cc-recent-comments li { width: 100%; float: left; list-style-type: none; }

#cc-recent-comments li img { float: left; margin-top: -5px; }

评论排行

把下面的代码复制到giisi-comment-central.php文档中:

<h2>Top Commenters</h2>

<ul>

<?php $sql = “SELECT comment_author, comment_author_url, comment_author_email, count(comment_ID)

as comment_count FROM $wpdb->comments WHERE comment_approved = ‘1’

AND comment_type not in (‘trackback’,’pingback’) GROUP BY comment_author, comment_author_url,

comment_author_email ORDER BY comment_count DESC LIMIT $max”;

$results = $wpdb->get_results($sql);

$template = ‘<a href=”%au”>%g %an</a> (%c comments)’;

$echoed = 0; foreach ($results as $row) { $tags = array(‘%g’,’%au’,’%an’,’%c’);

$replacements = array(get_avatar($row->comment_author_email,’32’),

$row->comment_author_url,

$row->comment_author,

$row->comment_count); echo ‘<li>’ . str_replace($tags,$replacements,$template) . ‘</li>’;

$echoed = 1; } if ($echoed==0) echo ‘<li>No commenter found.</li>’; ?> </ul>

当然,还有相关的CSS:

#cc-top-commenters li { width: 100%; float: left; list-style-type: none; }

#cc-top-commenters li img { float: left; margin-top: -5px; }

文章评论排行

<h2>Most Commented Posts</h2>

<ul>

<?php $sql = “SELECT p.*, c.comment_count FROM

$wpdb->posts p INNER JOIN (SELECT comment_post_id, count(comment_ID) as comment_count

from $wpdb->comments

WHERE comment_approved=’1′ GROUP BY comment_post_id) c ON (c.comment_post_id=p.ID)

ORDER BY c.comment_count DESC LIMIT $max”;

$results = $wpdb->get_results($sql);

$template = ‘<a href=”%pu”>%pt</a> (%c comments)’;

$echoed = 0; foreach ($results as $row) {

$tags = array(‘%pd’,’%pt’,’%pu’,’%c’);

$replacements = array($row->post_date,$row->post_title,get_permalink($row->ID),

$row->comment_count); echo ‘<li>’ . str_replace($tags,$replacements,$template) . ‘</li>’;

$echoed = 1; } if ($echoed==0)

echo ‘<li>No commenter found.</li>’;

?>

</ul>

OK,三个排行榜都添加了,就去后台新建一个页面选取相应的模板就可以了。

2 评论

发表回复