【问题标题】:posts_per_page=> 0 list all posts for a specific category in wordpressposts_per_page=> 0 列出 wordpress 中特定类别的所有帖子
【发布时间】:2019-03-26 19:07:32
【问题描述】:

我正在开发一个 wordpress 网页,我想在其中显示特定类别的零帖子。以下是代码:

<?PHP
   $temp_args = [
       '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) {
           }

       }
       wp_reset_postdata();
   }
   ?>

我添加了 Line#A 以控制特定类别的帖子数量。当 'posts_per_page' =&gt; 0 的值时,它会显示该特定特定类别的所有帖子列表,我不知道为什么。

问题陈述:

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

【问题讨论】:

  • 如果您不想显示某个类别的任何帖子,为什么要查找该类别的帖子?只是不要进行查找。将搜索包装在 if 块中。
  • @stevecomrie 你能在回答中告诉我我必须做什么吗?
  • 为什么不在您的原始查询中使用'category__not_in'
  • 你想让我在哪里使用'category__not_in'

标签: php wordpress


【解决方案1】:

这是 WordPress Core 中长期存在的错误。见Trac ticket #24142

您可以在运行代码之前通过将所有内容包装在 if 语句中来确保它大于 0:

<?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();
        }
    }
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多