【问题标题】:Multiple Wordpress loops, hide first loop on second page多个 Wordpress 循环,在第二页隐藏第一个循环
【发布时间】:2017-06-22 15:54:57
【问题描述】:

我已经构建了两个带有数字分页的 Wordpress 循环,如下所示:

 <div class="container" style="background:#ccc">
            <?
                $paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;

                // Custom Loop with Pagination 1
                // http://codex.wordpress.org/Class_Reference/WP_Query#Usage
                $args1 = array(
                    'paged'          => false,
                    'category_name' => 'latest',
                    'posts_per_page' => 1,
                );
                $query1 = new WP_Query( $args1 );

                while ( $query1->have_posts() ) : $query1->the_post();
                    the_title();
                    the_category(' ');
                    the_excerpt();
                endwhile;

            ?>
            <!-- second -->
            <?
                $args2 = array(
                    'paged'          => $paged2,
                    'category_name' => 'uncategorized',
                    'posts_per_page' => 2,
                );
                $query2 = new WP_Query( $args2 );

                while ( $query2->have_posts() ) : $query2->the_post();
                    the_title();
                    the_category(' ');
                    the_excerpt();
                endwhile;

                $pag_args2 = array(
                    'format'  => '?paged2=%#%',
                    'current' => $paged2,
                    'total'   => $query2->max_num_pages,
                    'add_args' => array( 'paged1' => $paged1 )
                );
                echo paginate_links( $pag_args2 );
            ?>
    </div>
    <!-- container -->

一切正常,但分页页面也显示了“最新”的帖子循环,尽管我已将分页设置为 false。 如何隐藏分页页面上的第一个循环? 建议表示赞赏。

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    在循环结束后使用 wp_reset_postdata() 函数 遵循此代码,可能会对您有所帮助

    <?php
    // example args
    $args = array( 'posts_per_page' => 3 );
    
    // the query
    $the_query = new WP_Query( $args );
    ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
        <!-- start of the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php the_title(); ?>
            <?php the_excerpt(); ?>
        <?php endwhile; ?><!-- end of the loop -->
    
        <!-- put pagination functions here -->
        <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    

    【讨论】:

    • 您好,不幸的是,添加后第一个循环在分页页面上仍然可见。
    猜你喜欢
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多