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

时间:2017-07-20 16:28:18

标签: php wordpress loops custom-post-type

我正在尝试覆盖默认的“博客页面最多显示”,设置为5个帖子。我有一个名为“常见问题解答”的自定义帖子类型,在查询中有参数'posts_per_page' => 999以获取此类型的所有帖子,但我似乎无法覆盖WordPress设置中的默认限制。我的FAQ查询代码如下所示,它可以在我的本地机器(MAMP)上运行,但不能在我上传到实时时使用。如何显示该类型的所有帖子?

                  <?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
                  ?>

1 个答案:

答案 0 :(得分:0)

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

<?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
}

干杯!