簡體   English   中英

WordPress獲得當前帖子類別中最受歡迎的帖子

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

我希望在我所有帖子的底部獲得最受歡迎的3個帖子。 我已經實現了。 但我確實需要顯示當前帖子的主要類別中最受歡迎的前三名。

我用過

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

獲得最受歡迎。 但是,當我添加類別時,它什么也不會返回:

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

我已經搜索了這個並且已經接近了,但是我懷疑我的php技能簡直太短了! 任何幫助,不勝感激。

要匹配類別,請嘗試以下操作:

$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