posts_per_page => 0列出wordpress中特定类别的所有帖子

时间:2019-03-26 19:07:32

标签: php wordpress

我正在处理一个wordpress网页,在该网页中我希望显示特定类别的零帖子。以下是该代码:

<?PHP
   $temp_args = [
       'post_type' => array('current-channel', 'post', 'current-episodes'),
       'post_status' => 'publish',
       'orderby' => array(
           'feat_yes' => 'ASC',
           'post_type' => 'ASC',
           'date' => 'DESC'),
       'posts_per_page' => $data->{"no_articles_".ICL_LANGUAGE_CODE},     // Line A
       'tax_query' => [
           [
               'taxonomy' => 'category',
               'field' => 'term_id',
               'terms' => $cat_today,
           ],
       ],

   ];
   echo '<pre>'; print_r($temp_args); echo '</pre>';
   $q = new WP_Query($temp_args);
   echo "Have posts: ";
   echo '<pre>'; print_r($q->have_posts()); echo '</pre>';
   if ($q->have_posts()) {
       while ($q->have_posts()) {
           $q->the_post();
           $post_type = strtolower(get_post_type());
           switch ($post_type) {
               case 'current-episodes':
                   get_template_part('template-parts/content-search', 'video');
                   break;
               case 'current-channel':
                   if (get_post_meta($post->ID, "current_portal_end_date_timestamp", true) > time()) {
                       echo "Hello World";
                       get_template_part('template-parts/content-search', 'channel');

                   }
                   break;
               case 'post':
                   get_template_part('template-parts/content', 'search');
                   break;
           }

       }
       wp_reset_postdata();
   }
   ?>

我添加了 Line#A ,以便控制特定类别的帖子数量。当值为'posts_per_page' => 0时,它将显示该特定类别的所有帖子列表,但我不确定为什么。

问题陈述:

我想知道当'posts_per_page' => 0时应该在上面的php代码中进行哪些更改,然后它应该显示零发布。

1 个答案:

答案 0 :(得分:1)

<?PHP
   if( $data->{"no_articles_".ICL_LANGUAGE_CODE} >= 1 ) {
     $temp_args = [
         'post_type' => array('current-channel', 'post', 'current-episodes'),
         'post_status' => 'publish',
         'orderby' => array(
             'feat_yes' => 'ASC',
             'post_type' => 'ASC',
             'date' => 'DESC'),
         'posts_per_page' => $data->{"no_articles_".ICL_LANGUAGE_CODE},     // Line A
         'tax_query' => [
             [
                 'taxonomy' => 'category',
                 'field' => 'term_id',
                 'terms' => $cat_today,
             ],
         ],

     ];
     echo '<pre>'; print_r($temp_args); echo '</pre>';
     $q = new WP_Query($temp_args);
     echo "Have posts: ";
     echo '<pre>'; print_r($q->have_posts()); echo '</pre>';
     if ($q->have_posts()) {
         while ($q->have_posts()) {
             $q->the_post();
             $post_type = strtolower(get_post_type());
             switch ($post_type) {
                 case 'current-episodes':
                     get_template_part('template-parts/content-search', 'video');
                     break;
                 case 'current-channel':
                     if (get_post_meta($post->ID, "current_portal_end_date_timestamp", true) > time()) {
                         echo "Hello World";
                         get_template_part('template-parts/content-search', 'channel');

                     }
                     break;
                 case 'post':
                     get_template_part('template-parts/content', 'search');
                     break;
             }

         }
         wp_reset_postdata();
     }
   }
   ?>