最近突然发现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———————
标签:Wordpress
不错,需要的时候再来你这里找