如何在 wordpress 中获取选定类别的热门帖子? [英] how to get popular posts fro selected categories in wordpress?
问题描述
我正在尝试使用评论计数来获取热门帖子.我还想从查询中排除一些类别.知道如何实现这一点.
排除特定类别的查询是什么?例如,我希望查询不包括类别名称 health 和 auto
<块引用>选择 ID、post_title、计数($wpdb->comments.comment_post_ID)AS 'stammy' FROM $wpdb->posts,$wpdb->comments WHERE comment_approved= '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_IDAND post_status = '发布' AND发布日期<'$now' AND post_date
WordPress 中有 3 个可用函数可以用来执行此操作.query_posts
、get_posts
或WP_Query
返回按评论数排序的帖子选择,不需要 SQL 查询..
query(数组('猫' =>'1,2,3,-4,-5,-6','orderby' =>'comment_count','订单' =>'降') );if( $my_query->have_posts() ) :while( $my_query->have_posts() ) : $my_query->the_post();?><div id="post-<?php the_ID(); ?>"<?php post_class();?>><?php the_title();?><br/><?php the_content();?>
<?php终了;万一;wp_reset_query();?>
1、2、3是要包含的类别,4、5和6是排除,负值表示排除,正常的非负值表示包含.
有关查询的其他可能参数,请参见此处.
http://codex.wordpress.org/Function_Reference/query_posts
这里也有关于 post 循环中使用的标签的信息(the_title
、the_content
等).http://codex.wordpress.org/Template_Tags#Post_tags
希望有帮助... :)
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
There are 3 available functions in WordPress you can use to do this.. query_posts
, get_posts
or WP_Query
to return a selection of posts ordered by the comment count, no need for the SQL query..
<?php
$my_query = new WP_Query;
$my_query->query( array(
'cat' => '1,2,3,-4,-5,-6',
'orderby' => 'comment_count',
'order' => 'desc'
) );
if( $my_query->have_posts() ) :
while( $my_query->have_posts() ) : $my_query->the_post();
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?><br />
<?php the_content(); ?>
</div>
<?php
endwhile;
endif;
wp_reset_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... :)
这篇关于如何在 wordpress 中获取选定类别的热门帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!