覆盖“最多显示博客页面”并显示自定义帖子类型的所有帖子

问题描述 投票:0回答:2

我正在尝试覆盖设置为 5 个帖子的默认“博客页面最多显示”。我有一个名为“FAQs”的自定义帖子类型,它在获取所有此类帖子的查询中有参数

'posts_per_page' => 999
,但是我似乎无法覆盖 WordPress 设置中的默认限制。我的 FAQ 查询代码如下,它可以在我的本地机器 (MAMP) 上运行,但当我将它上传到 live 时就不行了。我如何显示该类型的所有帖子?

                  <?php

                      wp_reset_query();

                      // Query for getting custom taxonomy 'FAQ Type' of custom post type 'FAQs'
                      $cat_args = array (
                        'taxonomy' => 'faq_type',
                        'exclude' => array(12),
                        'posts_per_page' => 999,
                        //'show_all' => true,
                        'orderby' => 'simple_page_ordering_is_sortable'
                      );
                      $categories = get_categories ( $cat_args );

                      foreach ( $categories as $category ) {

                        //wp_reset_query();

                        $cat_query = null;

                        // Query for getting posts of custom post type 'FAQs'
                        $args = array (
                          'post_type' => 'faq',
                          'faq_type' => $category->slug,
                          'posts_per_page' => 999,
                          //'show_all' => true,
                          'orderby' => 'simple_page_ordering_is_sortable',
                        );
                        $cat_query = new WP_Query( $args );

                        if ( $cat_query->have_posts() ) { ?>

                        <?php echo "<h2>". $category->name ."</h2>"; ?>

                        <ul id="resident-accordion" class="accordion white-bg-accordion" data-accordion data-allow-all-closed="true" role="tablist">

                        <?php

                          while ( $cat_query->have_posts() ) {
                            $cat_query->the_post();
                            ?>

                              <li class="accordion-item faq-content <?php //if ($firstLoop === true) { echo "is-active"; }?>" data-accordion-item>
                                <a href="#" class="accordion-title" role="tab"><?php the_title(); ?></a>

                                <div class="accordion-content" data-tab-content>
                                  <?php the_content(); ?>
                                </div>
                              </li>

                            <?php
                          } //wp_reset_query();
                          wp_reset_postdata(); //End WHILE

                        echo "</ul>";

                        } //End IF
                        wp_reset_postdata();
                        //wp_reset_query();
                      } //End FOR
                  ?>
php wordpress loops custom-post-type
2个回答
1
投票

您可以尝试使用下面的代码:

<?php
$cat_args = array(
    'taxonomy' => 'faq_type',
    'exclude'  => array(7),
    'orderby'  => 'simple_page_ordering_is_sortable'
);

$categories = get_terms( $cat_args );

foreach ( $categories as $category ) 
{
    $args = array(
        'post_type'      => 'faq',
        'posts_per_page' => -1, // load all posts
        'orderby'        => 'simple_page_ordering_is_sortable',
        'tax_query'      => array(
            array(
                'taxonomy' => 'faq_type',
                'field'    => 'slug',
                'terms'    => $category->slug
            )
        )
    );

    $cat_query = new WP_Query( $args );

    // enter the rest of your code below
}

或者您可以使用 get_posts() 接收帖子列表。

<?php
$cat_args = array(
    'taxonomy' => 'faq_type',
    'exclude'  => array(7),
    'orderby'  => 'simple_page_ordering_is_sortable'
);

$categories = get_terms( $cat_args );

foreach ( $categories as $category ) 
{
    $posts = get_posts(array(
        'numberposts'   => -1, // get all posts.
        'tax_query' => array(
            array(
                'taxonomy' => 'faq_type',
                'field'    => 'slug',
                'terms'     => $category->slug,
                'operator' => 'IN',
            ),
        ),
        'post_type'     => 'faq',
    ));

    // enter the rest of your code below
}

干杯!


0
投票

我希望你现在已经找到了答案,但你是如此接近。

代替:

'posts_per_page' => 999,

尝试:

'posts_per_page' => -1,

参见:分页参数

© www.soinside.com 2019 - 2024. All rights reserved.