【问题标题】:Override 'Blog pages show at most' and show all posts for custom post type覆盖“博客页面最多显示”并显示自定义帖子类型的所有帖子
【发布时间】:2017-07-20 16:28:17
【问题描述】:

我正在尝试覆盖设置为 5 个帖子的默认“博客页面最多显示”。我有一个名为“FAQs”的自定义帖子类型,它在查询中具有参数'posts_per_page' => 999,用于获取此类型的所有帖子,但是我似乎无法覆盖 WordPress 设置中的默认限制。我的常见问题解答查询代码如下,它适用于我的本地机器(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
                  ?>

【问题讨论】:

    标签: php wordpress loops custom-post-type


    【解决方案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
    }
    

    干杯!

    【讨论】:

    • 很遗憾,这不起作用,仍然显示最大帖子数 5
    • @Amesey 我已经更新了我的答案,您可以查看一下。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2022-07-20
    • 1970-01-01
    相关资源
    最近更新 更多