显示类别中最受欢迎的帖子

时间:2017-01-10 作者:user6738171

现在,在我的滑块中,我将按视图显示最受欢迎的帖子。然而,我想通过浏览量来购买最受欢迎的帖子,只针对某个类别?我要使用的类别id是“10”。我将如何实现这一点?提前谢谢。

<div id="slider-food"> 
<?php 
$carousel_cat = get_theme_mod(\'carousel_setting\',\'1\'); 
$carousel_count = get_theme_mod(\'count_setting\',\'4\'); 
$month = date(\'m\'); 
$year = date(\'Y\'); 
$new_query = new WP_Query( array(\'posts_per_page\' => $carousel_count, \'meta_key\' => \'wpb_post_views_count\', \'orderby\' => \'meta_value_num\', \'order\' => \'DESC\',\'monthnum\'=>$month,\'year\'=>$year )); 
?> 
<?php if ( $new_query->have_posts() ) : ?> 
<?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?> 
<div class="item"> 
    <?php the_post_thumbnail(\'popular-posts\'); ?>
    <h2><a class="popular-category" 
        <?php 
        $categories = get_the_category(); 
        $separator = ", ";
        $output = \'\';

        if ($categories) {
            foreach ($categories as $category) {
                $output .= \'<a href="\' . get_category_link($category->term_id) . \'">\' . $category->cat_name . \'</a>\' . $separator;
            }
            echo trim($output, $separator);
        }

        ?></a></h2>
 <p>
     <a class="popular-excerpt" href="<?php the_permalink(); ?>"><?php echo get_the_excerpt(); ?></a>
                </p>
</div> 
<?php endwhile; wp_reset_postdata(); ?> 
<?php else : ?> 
<p><?php _e( \'Sorry, No Popular Posts Found \' ); ?></p> 
<?php endif; ?> 
</div>

1 个回复
最合适的回答,由SO网友:prosti 整理而成

添加怎么样\'cat\'=> 10 你的问题?

$new_query = new WP_Query(  array(
   \'posts_per_page\' => $carousel_count, 
   \'meta_key\' => \'wpb_post_views_count\', 
   \'orderby\' => \'meta_value_num\', 
   \'order\' => \'DESC\',
   \'monthnum\'=> $month,
   \'year\'=> $year, 
   \'cat\'=> 10
)); 

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请