第二个帖子循环重复相同的帖子

时间:2018-04-30 15:36:09

标签: php wordpress

我已经阅读了多个关于重复同一帖子的多个循环的问题,即使我已经尝试了建议的答案,但我似乎无法解决为什么我的第二个循环会重复相同的帖子。 / p>

初始循环很好,理想情况下我需要保留HTML结构以用于设计目的,但我无法弄清楚为什么我的第二个循环将重复相同的帖子。

<div class="latest-post-feed">
    <?php
       global $post;
       $blog = array( 'posts_per_page' => '4', 'offset' => '7');
       $blogPosts = get_posts( $blog );
    ?>
       <div class="post-list row">
          <div class="post-column column-left">
             <?php $i = 1; foreach ( $blogPosts as $post ) : setup_postdata( $post ); ?>
                <?php if($i == 2): // splits it out into 1 x 2 column ?>
                  </div><!-- .column -->
                  <div class="post-column column-middle">
                <?php endif; ?>

                <?php if($i == 4): // splits it out into 1 x 1 column ?>
                   </div><!-- .column -->
                   <div class="post-column column-right">
                <?php endif; ?>
                   <?php get_template_part( 'template-parts/content-latest', get_post_format() ); ?>
                <?php $i++; endforeach; ?>
             </div><!-- .post-column -->
          </div><!-- .post-list -->
       <?php wp_reset_postdata();  wp_reset_query(); ?>
  </div><!-- .latest-post-feed -->

  <div class="latest-post-feed">
     <?php
       global $_post;
       $_blog = array( 'posts_per_page' => '7', 'offset' => '-1' );
       $_blogPosts = get_posts( $_blog );
     ?>
       <div class="post-list row">
         <div class="post-column column-left-bottom">
           <?php $x = 1; foreach ( $_blogPosts as $_post ) : setup_postdata( $_post ); ?>
             <?php if($x == 2): // splits it out into 3 x 3 column ?>
                </div><!-- .column -->

                <div class="post-column column-right-bottom">
             <?php endif; ?>
                <?php get_template_part( 'template-parts/content-latest-2', get_post_format() ); ?>
           <?php $x++; endforeach; ?>
         </div>
       </div>
    <?php wp_reset_postdata();  wp_reset_query(); ?>
 </div>

输出始终是: https://i.stack.imgur.com/jzcCi.png

我尝试做的是创建第二个循环,该循环从第一个循环的偏移量开始继续。理想情况下,需要总共显示11个帖子,第一个循环显示4,第二个循环显示7,跟随第一个循环的数字顺序。

2 个答案:

答案 0 :(得分:0)

抱歉没有足够的时间来使用您的代码,但也许这会对您有帮助(这就是我在主题上的表现):
循环1:

<?php query_posts('posts_per_page=3'); $images = '0'; ?>

<?php while (have_posts()) : the_post(); $images++; $image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
    <div class="carousel-item <?php if($images == '1'){ echo 'active';} ?>">
        <div class="gradient"></div>
        <img class="d-block w-100" src="<?php echo $image_src[0]; ?>" alt="<?php the_title(); ?>">
        <div class="carousel-caption d-none d-md-block">
            <h1><?php the_title(); ?></h1>
            <p><a href="<?php the_permalink(); ?>" class="btn btn-tobi">Artikel lesen</a></p>
        </div>
    </div>
<?php endwhile;?>

循环2:

<?php
$loop = new WP_Query(array('offset' => 3));

if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();  $title = get_the_title();?>

<div class="card border-dark">
    <?php the_post_thumbnail( 'medium_large', array( 'class' => 'card-img-top', 'alt' => $title  ) ); ?>
        <div class="card-body">
            <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <p class="card-text"><?php echo wp_trim_words( get_the_content(), 40, '...' ); ?></p>
        </div>
        <div class="card-footer">
            <small class="text-muted"><?php _e( 'Kategorie:' ); ?> <?php the_category( ', ' ); ?><br>Gepostet am: <?php the_time('d.m.Y'); ?></small>
        </div>
</div>

<?php endwhile; else : ?>
    <p class="sry"><?php esc_html_e( 'Sorry, dazu habe ich wohl noch nichts geschrieben.' ); ?></p>

<?php endif; ?>

答案 1 :(得分:0)

<div class="latest-post-feed">
    <?php
       global $post;
       $blog = array( 'posts_per_page' => '4', 'offset' => '7');
       $blogPosts = get_posts( $blog );
    ?>
       <div class="post-list row">
          <div class="post-column column-left">
             <?php $i = 1; foreach ( $blogPosts as $post ) : setup_postdata( $post );
**$do_not_duplicate = $post->ID;** ?>
                <?php if($i == 2): // splits it out into 1 x 2 column ?>
                  </div><!-- .column -->
                  <div class="post-column column-middle">
                <?php endif; ?>

                <?php if($i == 4): // splits it out into 1 x 1 column ?>
                   </div><!-- .column -->
                   <div class="post-column column-right">
                <?php endif; ?>
                   <?php get_template_part( 'template-parts/content-latest', get_post_format() ); ?>
                <?php $i++; endforeach; ?>
             </div><!-- .post-column -->
          </div><!-- .post-list -->
       <?php wp_reset_postdata();  wp_reset_query(); ?>
  </div><!-- .latest-post-feed -->

  <div class="latest-post-feed">
     <?php
       global $_post;
       $_blog = array( 'posts_per_page' => '7', 'offset' => '-1' );
       $_blogPosts = get_posts( $_blog );
     ?>
       <div class="post-list row">
         <div class="post-column column-left-bottom">
           <?php $x = 1; foreach ( $_blogPosts as $_post ) : setup_postdata( $_post );
 **if( $post->ID == $do_not_duplicate ) continue;** ?>
             <?php if($x == 2): // splits it out into 3 x 3 column ?>
                </div><!-- .column -->

                <div class="post-column column-right-bottom">
             <?php endif; ?>
                <?php get_template_part( 'template-parts/content-latest-2', get_post_format() ); ?>
           <?php $x++; endforeach; ?>
         </div>
       </div>
    <?php wp_reset_postdata();  wp_reset_query(); ?>
 </div>

添加$ do_not_duplicate = $ post-&gt; ID;在foreach的第一个循环之后,然后添加if($ post-&gt; ID == $ do_not_duplicate)继续;在第二轮foreach之后。