Wordpress循环问题:多个循环,index.php和被分页,导致下一页出现重复帖子

Wordpress循环问题:多个循环,index.php和被分页,导致下一页出现重复帖子,wordpress,loops,pagination,Wordpress,Loops,Pagination,大家好。我不确定我所经历的是一个bug(由于最近升级到3.1.2)还是糟糕的编码。自从我升级到3.1.2版后,我的索引页上出现了两个循环的问题 这是我为索引页准备的内容 <?php if ( ! is_paged() && is_front_page() ) { echo '<h6 class="sec1 title">FEATURE</h6>'; $sticky = get_optio

大家好。我不确定我所经历的是一个bug(由于最近升级到3.1.2)还是糟糕的编码。自从我升级到3.1.2版后,我的索引页上出现了两个循环的问题

这是我为索引页准备的内容

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


//导航到这里来
例如,第一个循环(粘性帖子)-不分页,产生3篇帖子,第二个循环(所有其他帖子)-分页,产生10篇帖子。我遇到的问题是,当我转到下一页时,第1页第二个循环中的最后3篇文章会在第2页顶部重复

注意:第一个循环仅在第1页上,第二页不会重复,这正是我想要的

这就是我尝试的,我删除了(!is_paged()&&is_front_page)条件以及整个第一个循环,问题得到了解决


我做错了什么?

在第一次循环之后,尝试添加 wp_reset_postdata()

我不确定您是否只尝试在第一页上有第一个循环,但是如果您是,请尝试类似的方法

$paged = get_query_var('page');

if ($paged < 2) :
     // Put whatever you want to only show up on the first page here
endif;
$paged=get_query_var('page');
如果($paged<2):
//把你想要的东西放在第一页
endif;
谢谢克里斯

我改变了你的建议(这似乎不起作用)

$paged=get_query_var('page');
如果($paged<2):
//把你想要的东西放在第一页
endif;

$paged=get_query_var('paged');
如果($paged<1){
//代码在这里
}
似乎第一页没有被视为“分页”。。“分页”仅适用于第一页以外的页面

这是为感兴趣的任何人更新的代码。给克里斯小费。再次感谢

$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; ?>
$paged=get_query_var('paged');
如果($paged<1){
回声“特征”;
$sticky=get_选项('sticky_posts');
如果(isset($sticky[0])){
$args=数组(
“每页帖子数”=>3,
'post_uuin'=>$sticky,
“忽略帖子”=>1);
//质疑
$featured\u query=新的WP\u查询($args);
而($featured\u query->have\u posts()):
$featured_query->the_post();
获取模板部分(“内容”、“特色”);
结束时;
wp_reset_postdata();
}//endif粘性
}//endif$paged
?>

上一个例子的另一个选择是在Chris回答之前我从头开始构建的



这篇文章解决了我的多循环分页问题。

我必须承认,第二个问题基纳看起来有点难看,但它确实起作用,我还没有彻底测试过,这是一个临时解决方案,我花了几分钟才写出来。
$paged = get_query_var('paged');

if ($paged < 1 ) {
   // code goes here
}
$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; ?>
        <?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; ?>