Loading...

关于wordpress无法显示评论最多文章的解决办法

最近突然发现sidebar的热门(即评论最多)文章的功能失效了,以为是插件和wp2.6不兼容,所以懒得去理,结果今天打开我的模板文件,发现了这样一行:

$myposts = get_posts('numberposts=15&offset=0&orderby=comment_count&order=DESC');

看来并不是插件的问题,于是进入wp的codex查看get_post标签是不是又有变化,结果发现orderby并不支持comment_count的排序方式:

$orderby
(string) (optional) Sort posts by one of various values, including:

* ‘author’ - Sort by the numeric author IDs.
* ‘category’ - Sort by the numeric category IDs.
* ‘content’ - Sort by content.
* ‘date’ - Sort by creation date.
* ‘ID’ - Sort by numeric post ID.
* ‘menu_order’ - Sort by the menu order. Only useful with pages.
* ‘mime_type’ - Sort by MIME type. Only useful with attachments.
* ‘modified’ - Sort by last modified date.
* ‘name’ - Sort by stub.
* ‘parent’ - Sort by parent ID.
* ‘password’ - Sort by password.
* ‘rand’ - Randomly sort results.
* ’status’ - Sort by status.
* ‘title’ - Sort by title.
* ‘type’ - Sort by type.

于是再次放google搜索,发现了wp论坛的这篇文章

comment_count does indeed increment when you add a comment, there’s no changes you should need to make for that.

But you cannot orderby comment_count, because nobody has ever wanted to do that ever before. So, in query.php on line 943, you’ll find this:
$allowed_keys = array(’author’, ‘date’, ‘category’, ‘title’, ‘modified’, ‘menu_order’);

Add comment_count to that array and then your orderby should work

终于找到原因了,但是2.6的修改却不一样,对于2.6,正确的修改应该是这样,找到/wp-includes/query.php,修改1254行如下:

$allowed_keys = array('author', 'date', 'category', 'title', 'modified', 'menu_order', 'parent', 'ID', 'comment_count', 'rand' );

comment_count一定要加到中间,然后再将1264行道1274行的代码替换如下:

switch ($orderby) {
case 'menu_order':
break;
case 'ID':
$orderby = "$wpdb->posts.ID";
break;
case 'comment_count':
$orderby = "$wpdb->posts.comment_count";
break;
case 'rand':
$orderby = 'RAND()';
break;
default:
$orderby = "$wpdb->posts.post_" . $orderby;

修改完成!
如果你是一个懒虫,请直接下载这个文件,覆盖/wp-includes/query.php即可,于是乎,评论最多文章的功能又出来了.
query.php下载地址:query

————————–end———————

标签:
发表于 2008-09-04 11:10:28 目录:Web技术|WEB Tech, Wordpress [RSS 2.0] 你可以发表评论, 或者从您的网站 trackback
feed url
feed url

前3排已经被占据了 快抢好位置哦

  • 1楼 猪头 在 2008.09.04 17:29发表评论如下:

    不错,需要的时候再来你这里找

  • 2楼 NetPuter 在 2008.09.04 23:00发表评论如下:

    很好很强大哦!!
    留着以后用~

  • 3楼 maqingxi 在 2008.10.17 15:24发表评论如下:

    所以说,自己架设一个博客是多么的不容易,要经常调整。谢谢了。

  • 转到第
(Required)
(Required, not published)
如果留言未显示请不要重复留言,我将为你恢复!