简体   繁体   English

WordPress获得当前帖子类别中最受欢迎的帖子

[英]Wordpress get most popular posts in current post category

I'm looking to get the top 3 most popular posts on the bottom of all my posts. 我希望在我所有帖子的底部获得最受欢迎的3个帖子。 Which I've achieved. 我已经实现了。 But I really need to display the top 3 most popular within the current post's main category. 但我确实需要显示当前帖子的主要类别中最受欢迎的前三名。

I've used 我用过

<?php $popular = new WP_Query(array(
         'posts_per_page'=>3, 
         'meta_key'=>'popular_posts', 
         'orderby'=>'meta_value_num', 
         'order'=>'DESC'
         ));

to get most popular. 获得最受欢迎。 But when I add in categories, it returns nothing: 但是,当我添加类别时,它什么也不会返回:

<?php $popular = new WP_Query(array( 
         'posts_per_page'=>3, 
         'cat' => 60, 
         'meta_key'=>'popular_posts', 
         'orderby'=>'meta_value_num', 
         'order'=>'DESC'));

I've searched and searched for this and I've come close but I suspect my php skills are woefully short! 我已经搜索了这个并且已经接近了,但是我怀疑我的php技能简直太短了! Any help greatly appreciated. 任何帮助,不胜感激。

to match the category try this: 要匹配类别,请尝试以下操作:

$args = array(
     'posts_per_page' => '3',
     'meta_key'=>'popular_posts', 
     'orderby'=>'meta_value_num', 
     'order'=>'DESC',
     'tax_query' => array(
                        array(
                            'taxonomy' => 'category',
                            'field' => 'id',
                            'terms' => array ( 60 ) //you can add more comma separated category ids here
                        )
                    )
                );
$popular = new WP_Query( $args );
if (  $popular->have_posts() ) :
     while ( $popular->have_posts() ) : $popular->the_post(); 
          echo '<div class="entry-content">';
          echo '<h2 class="entry-title main_title">'.get_the_title().'</h2>';
          the_content();
          echo '</div><!-- .entry-content -->';
     endwhile;
endif; 
wp_reset_query();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM