WordPress循环问题:多个循环,index.php和is_paged导致下一页重复发帖 [英] Wordpress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next page

查看:117
本文介绍了WordPress循环问题:多个循环,index.php和is_paged导致下一页重复发帖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我不确定我遇到的是错误(由于最近升级到3.1.2)还是编码不良导致的.自从我升级到版本3.1.2以来,我的索引页面上就遇到了两个循环的问题.

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

例如,第一个循环(粘性帖子)-未分页,产生 3个帖子,第二个循环(所有其他帖子)-已分页,产生 10个帖子.我遇到的问题是,当我移至下一页时,第1页第二个循环中的最后 3个帖子在第2页顶部重复出现.

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.

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

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

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

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.

我在做什么错了?

推荐答案

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

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;

这篇关于WordPress循环问题:多个循环,index.php和is_paged导致下一页重复发帖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆