Wordpress loop problem: Multiple loops, index.php

2019-09-03 09:26发布

Hey everyone. I'm not sure if what I'm experiencing is a result of a bug (due to the recent upgrade to 3.1.2) or poor coding. Ever since i upgraded to version 3.1.2 ive been experiencing a problem with two loops on my index page.

Here's what I've got for my index page

<?php
        if ( ! is_paged() && is_front_page() ) {
            echo '<h6 class="sec1 title">FEATURE</h6>';
            $sticky = get_option( 'sticky_posts' );
            if ( isset( $sticky[0] ) ) {
                $args = array(
                    'posts_per_page' => 3,
                    'post__in'  => $sticky,
                    'ignore_sticky_posts' => 1);
                // Query
                $featured_query = new WP_query( $args );
                while ($featured_query->have_posts() ) :
                $featured_query->the_post();
                    $featured[] = $post->ID;

                get_template_part( 'content', 'featured' );

                endwhile;
            } // endif sticky
        } // endif is_paged
        ?>

        <?php
            $sticky = get_option( 'sticky_posts' );
            echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
            $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
            $query_args = array(
                'posts_per_page' => 10,
                'paged' => $paged,
                'post__not_in' => $featured,
                'post__not_in' => $sticky
                );

            query_posts($query_args);
            if (have_posts() ) :
            while (have_posts() ) :
            the_post();

            get_template_part( 'content', get_post_format() );
        ?>

        <!--<?php trackback_rdf(); ?>-->

        <?php endwhile; else: ?>

        <div class="box">
            <p>
                <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
            </p>
        </div>

        <?php endif; ?>

// Navigation comes over here

Say for example the first loop (sticky posts) - which IS NOT paged, yields 3 posts, and the second loop (all other posts) - which IS paged, yields 10 posts. The problem I'm experiencing is that when i move to the next page, the last 3 posts from the second loop on page 1 get repeated at the the top of page 2.

Note: The first loop is only on page 1, and doesn't get repeated on the second page, which is what i intended.

So this is what i tried, i removed the ( ! is_paged() && is_front_page ) condition along with the entire first loop, and the problem got resolved.

What am i doing wrong?

3条回答
混吃等死
2楼-- · 2019-09-03 09:32

After your first loop, try adding wp_reset_postdata();

I'm not sure if your trying to only have the first loop on the first page, but if you are, try something like

$paged = get_query_var('page');

if ($paged < 2) :
     // Put whatever you want to only show up on the first page here
endif;
查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-03 09:43

Thanks Chris,

I changed your suggestion (which didnt seem to work)

$paged = get_query_var('page');

if ($paged < 2) :
 // Put whatever you want to only show up on the first page here
endif;

to

$paged = get_query_var('paged');

if ($paged < 1 ) {
   // code goes here
}

It seems as if the first page isnt considered "paged".. "paged" only applies to pages beyond the first page.

this is the updated code for anyone who's interested. Hat tip to Chris. Thanks again.

$paged = get_query_var('paged');

if ($paged < 1 ) {
    echo '<h6 class="sec1 title">FEATURE</h6>';
    $sticky = get_option( 'sticky_posts' );
    if ( isset( $sticky[0] ) ) {
        $args = array(
            'posts_per_page' => 3,
            'post__in'  => $sticky,
            'ignore_sticky_posts' => 1);
        // Query
        $featured_query = new WP_query( $args );
        while ($featured_query->have_posts() ) :
        $featured_query->the_post();

        get_template_part( 'content', 'featured' );

        endwhile;

        wp_reset_postdata();
    } // endif sticky
} // endif $paged
?>

<?php
    $sticky = get_option( 'sticky_posts' );
    echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
    $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
    $query_args = array(
        'posts_per_page' => 10,
        'paged' => $paged,
        'post__not_in' => $sticky
        );

    query_posts($query_args);
    if (have_posts() ) :
    while (have_posts() ) :
    the_post();

    get_template_part( 'content', get_post_format() );
?>

<!--<?php trackback_rdf(); ?>-->

<?php endwhile; else: ?>

<div class="box">
    <p>
        <?php _e( 'Sorry, no posts matched your criteria.' ); ?>
    </p>
</div>

<?php endif; ?>

an alternative to the previous example was one which i built from the ground up before Chris answered is

        <?php if ( isset( $sticky[0] ) && ! is_paged() ) {
                echo '<h6 class="sec1 title">FEATURE</h6>'; 
        } ?>

        <?php while ( have_posts() ) : the_post(); ?>

            <?php if ( is_sticky() ) {
                get_template_part( 'content', 'featured' );             
            } ?>

        <?php endwhile; ?>

        <?php rewind_posts(); ?> 

        <?php
        echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';

        global $sticky;
        $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
        $args = array(
                'posts_per_page' => 10,
                'paged' => $paged,
                'post__not_in' => $sticky
            );
            query_posts( $args );
            while ( have_posts() ) :
            the_post() ;
        ?>

        <?php get_template_part( 'content', get_post_format() ); ?>

        <!--<?php trackback_rdf(); ?>-->

        <?php endwhile; ?>
查看更多
登录 后发表回答