如何获取 WordPress 中选定类别的热门帖子?
我正在尝试使用评论数来获取热门帖子。我还想从查询中排除某些类别。知道如何实现这一点。
查询什么来排除特定类别?例如,我希望查询不应包含类别名称 health 和 auto
选择 ID、帖子标题、 COUNT($wpdb->comments.comment_post_ID) AS 'stammy' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = '发布' AND 发布日期 < '$now' 和 post_date
I am trying to get popular posts using the coment count. I also want to exclude some categories from the query. Any idea how this can be achieved.
What would be query to exclude particular categories? for example i want the query should not include category names health and auto
SELECT ID, post_title,
COUNT($wpdb->comments.comment_post_ID)
AS 'stammy' FROM $wpdb->posts,
$wpdb->comments WHERE comment_approved
= '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID
AND post_status = 'publish' AND
post_date < '$now' AND post_date
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WordPress 中有 3 个可用函数可以用来执行此操作。
query_posts
、get_posts
或WP_Query
返回按以下顺序排序的帖子选择评论计数,无需SQL查询。1、2、3是要包含的类别,4、5、6是排除,负值表示排除,正常非负数是包含。
请参阅此处了解查询的其他可能参数。
http://codex.wordpress.org/Function_Reference/query_posts
这里还提供有关内部使用的标签的信息后循环(
the_title
、the_content
等)。http://codex.wordpress.org/Template_Tags#Post_tags
希望有所帮助...:)
There are 3 available functions in WordPress you can use to do this..
query_posts
,get_posts
orWP_Query
to return a selection of posts ordered by the comment count, no need for the SQL query..1, 2 and 3 are categories to include, 4, 5 and 6 are exclusions, the negative value indicates an exclusion, normal non-negatives are inclusions.
See here for other possible parameters for the query.
http://codex.wordpress.org/Function_Reference/query_posts
Also here for information on tags used inside the post loop(
the_title
,the_content
, etc).http://codex.wordpress.org/Template_Tags#Post_tags
Hope that helps... :)
我不久前就做过这个。
在我的博客上你有解决方案,我知道它是波兰语,但代码就是代码:) http://blog.grabek-adam.pl/2010/06/wordpress-popularne-posty-plugin-do-obrazkw/
这里您刚刚查询:
此代码只会包括您在此处指定的类别
AND wp_term_taxonomy.term_id IN ("cat_id1,cat_id2,cat_id5")
但我认为很容易根据您的要求进行更改I done this while time ago.
On my blog You have solution, I know its in polish but code is code :) http://blog.grabek-adam.pl/2010/06/wordpress-popularne-posty-plugin-do-obrazkw/
Here You have just query:
This code will only include categories that You specify in here
AND wp_term_taxonomy.term_id IN ("cat_id1,cat_id2,cat_id5")
but i think it will be easy to change for Your requirements