分页不适用于页面上的自定义查询

时间:2021-05-20 作者:Cheo Molina

我在一个页面上有一个自定义查询(它不是存档页面),它工作得很好,但我想要分页,它根本不工作。。。

                        <?php 
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                                if(have_posts() ):
   $args = array( \'post_type\' => \'store\',
                  //    \'posts_per_page\' => 9,
                   \'paged\' => $paged,
                  \'meta_key\' => \'store_client_type\',
                  \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'style\',
                            \'field\'    => \'slug\',
                            \'terms\'    => $style,
                        )
                    ),
                 \'meta_query\' => array(
                        \'relation\' => \'AND\',
                        array(
                        \'plan_clause\' => array(
                                        \'key\'  => \'store_client_type_plan\',
                                        \'compare\'   => \'EXISTS\',
                                        ),
                                    ),
                        array(
                        \'relation\' => \'AND\',
            )
                                ),
                  \'orderby\' => array(
                                \'plan_clause\'  => \'desc\',
                            )   
                 ); 
    $loop = new WP_Query( $args );
        while($loop->have_posts()):  $loop->the_post(); ?>
                            
                                <div class="col-lg-4 col-md-6 col-sm-12 center">
                                    <div class="blog-article-card pt-2">
                                        <h4><?php the_title(); ?></h4>
                                        </div>
                                    </div>
                                </div>
                                <?php     
        endwhile;  
        endif;
                the_posts_pagination(); 
        wp_reset_query();   
                    ?> 

1 个回复
最合适的回答,由SO网友:Cheo Molina 整理而成

使用paginate\\u链接的魔力解决(在endwhile之后)

<?php
$big = 999999999; // need an unlikely integer
 
echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $loop->max_num_pages
) ); ?>