Wordpress循环-每两个自定义帖子不同的HTML


wordpress loop – different html for every 2nd custom post

我正在寻找一个解决方案来设置我的自定义帖子循环,像这样:

第一篇> img left, content right//第二篇> content left, img right

这是我到目前为止的代码:

<div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) :  ?>
   <article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
                <div class="row centered wow fadeInUpBig" data-wow-duration="2s">   
                    <div class="col col-4">
                           <?php the_post_thumbnail(600); ?>
                </div>
                <div class="col col-6">
                <section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span> 
                       <h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
                  <?php the_content();?>
                </section> 
                 </div>
                </div>
              </article>
<?php else : ?>
   <article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
                <div class="row centered wow fadeInUpBig" data-wow-duration="2s">  
                                <div class="col col-6">
                <section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span> 
                       <h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
                  <?php the_content();?>
                </section> 
                 </div>
                    <div class="col col-4">
                           <?php the_post_thumbnail(600); ?>
                </div>
                </div>
              </article>
<?php endif; endwhile; endif; ?>
</div>

我知道,这个问题已经问过几次了,我已经试过了这个和这个,我读了这个,但没有什么对我有效。我做错了什么?

我想象它只输出一个帖子,该帖子实际上是附加到页面的帖子内容。问题在于如何初始化页面内的附加循环。您正在创建一个新的post对象-但您没有将其分配给if/while语句:

<?php if (have_posts()) : while(have_posts()) : the_post(); $i++; if(($i % 2) == 0) :  ?>
应:

<?php if ($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); $i++; if(($i % 2) == 0) :  ?>

注意,这里添加了$loop变量,用于设置post对象和参数。

我得到了这段代码(未更改)已经工作:

                <div class="container">
<?php $loop = new WP_Query( array( 'post_type' => 'profile', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
 <article id="post-<?php the_ID(); ?>" <?php post_class(''); ?> role="article" itemscope itemprop="blogPost" itemtype="http://schema.org/BlogPosting">
                <div class="row centered wow fadeInUpBig" data-wow-duration="2s">   
                    <div class="col col-4">
                           <?php the_post_thumbnail(600); ?>
                </div>
                <div class="col col-6">
                <section class="entry-content cf" itemprop="articleBody">
<span class="bold function"><?php echo get_the_term_list( $post->ID, 'Funktion', '', ', ', '' ); ?></span> 
                       <h2 class="entry-title single-title" itemprop="headline" rel="bookmark"><?php the_title(); ?></h2>
                  <?php the_content();?>
                </section> 
                </div><!--.end col-->
                </div><!--.end row-->
              </article>
<?php endwhile; wp_reset_query(); ?>
                </div><!--.end container-->