基本上我需要一个自定义的 Wordpress 帖子循环,它以以下格式输出帖子(每两个帖子都应该以这种方式格式化,所以两个帖子都在一个“col”div 中,都包含在“row”div 中。):
<div class="row cols2">
<div class="col left">
<a href="#"><img src="featured-image.jpg" /></a>
<h2><a href="#">Blog Post Title</a></h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</div><!-- /.col left -->
<div class="col right">
<a href="#"><img src="featured-image.jpg" /></a>
<h2><a href="#">Blog Post Title</a></h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
</div><!-- /.col right -->
</div><!-- /.row cols2 -->
我使用以下循环或多或少地实现了这一点:
<?php $counter=1;$grids=2;global $query_string;query_posts($query_string.'&caller_get_posts=1&posts_per_page=6');if(have_posts()):while(have_posts()):the_post();if($counter==1):?>
<div class="row cols2">
<div class="col left">
<a href="<?php the_permalink()?>"><?php the_post_thumbnail('default-thumb')?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt() ?>
</div><!-- /.col left -->
<?php elseif($counter==$grids):?>
<div class="col right">
<a href="<?php the_permalink()?>"><?php the_post_thumbnail('default-thumb')?></a>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt() ?>
</div><!-- /.col right -->
</div><!-- /.row cols2 -->
<?php $counter=0;endif;$counter++;endwhile;endif;?>
唯一的问题是当“.row cols2” div 中只有一个帖子时,它会搞砸,因为“.row cols2”结束标记仅在连续有两个帖子时输出。如果有人对如何提供帮助有任何想法,将不胜感激!