【问题标题】:Wordpress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next pageWordpress 循环问题:多个循环、index.php 和 is_paged 导致下一页出现重复帖子
【发布时间】:2011-08-16 03:19:59
【问题描述】:

大家好。我不确定我遇到的问题是由于错误(由于最近升级到 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) 条件以及整个第一个循环,问题得到了解决。

我做错了什么?

【问题讨论】:

    标签: wordpress loops pagination


    【解决方案1】:

    在您的第一个循环之后,尝试添加 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;
    

    【讨论】:

      【解决方案2】:

      谢谢克里斯,

      我改变了你的建议(似乎没有用)

      $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('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; ?>
      

      上一个示例的替代方案是我在 Chris 回答之前从头开始构建的示例

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

      【讨论】:

      • 我必须承认第二个 kina 看起来有点难看,但它确实有效,我还没有彻底测试过,这是一个临时解决方案,我花了几分钟才写出来。
      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多