我编写了一个代码,可以填充我在代码中使用的引导网格结构,而for循环则是在第二行重复它的值
上图告诉您第一行的第2列和第3列以及第二行的第1列和第2列具有相同的值,这是我的代码
<div class="row">
<div class="col-xs-12">
<div class="rst-mediagrid">
<?php
global $post;
$loop = new WP_Query( array( \'posts_per_page\' => 9,\'orderby\'=>rand) );
$posts = array();
while ( $loop->have_posts() ) :
$items = array();
$items[\'link\']=wp_get_attachment_url( get_post_thumbnail_id( $post->ID ));
$items[\'Image\'] = get_the_post_thumbnail($loop->the_post());
$items[\'LinkPost\']=get_permalink($post->ID);
$items[\'Title\']=get_the_title($post->ID);
$items[\'PostTime\']=get_the_time(\'M d,Y\', $post->ID);
array_push($posts, $items);
endwhile;
for($i = 1; $i< count($posts); $i++){
?>
<?php
if($i==1){
?>
<div class="div">
<div class="rst-col rst-col-50">
<div class="rst-postpic">
<a href="<?php echo $posts[$i][\'LinkPost\']; ?>"><img src="<?php echo $posts[$i+1][\'link\']; ?>" alt="" style="height: 385px;width: 770px"/></a>
<?php echo $i+1; ?>
</div>
<div class="rst-postinfo">
<a href="#"><span>Sport</span></a>
<h6><a href="<?php echo $posts[$i][\'LinkPost\']; ?>"><?php echo $posts[$i][\'Title\']; ?></a></h6>
<time><i class="fa fa-clock-o"></i><?php echo $posts[$i][\'PostTime\']; ?></time>
</div>
</div>
<?php //endif; ?>
<div class="rst-col rst-col-25">
<div class="rst-postpic rst-postvideo">
<a href="<?php echo $posts[$i+1][\'LinkPost\']; ?>"><img src="<?php echo $posts[$i+2][\'link\']; ?>" alt="" style="height: 385px;width: 770px"/></a>
<?php echo $i+2; ?>
</div>
<div class="rst-postinfo">
<a href="#"><span>Sport</span></a>
<h6><a href="<?php echo $posts[$i+1][\'LinkPost\']; ?>"><?php echo $posts[$i+1][\'Title\']; ?></a></h6>
<time><i class="fa fa-clock-o"></i><?php echo $posts[$i+1][\'PostTime\']; ?></time>
</div>
</div>
<div class="rst-col rst-col-25">
<div class="rst-postpic">
<a href="<?php echo $posts[$i+2][\'LinkPost\']; ?>"><img src="<?php echo $posts[$i+3][\'link\']; ?>" alt="" style="height: 385px;width: 770px"/></a>
<?php echo $i+3; ?>
</div>
<div class="rst-postinfo">
<a href="#"><span>Sport</span></a>
<h6><a href="<?php echo $posts[$i+2][\'LinkPost\']; ?>"><?php echo $posts[$i+2][\'Title\']; ?></a></h6>
<time><i class="fa fa-clock-o"></i><?php echo $posts[$i+2][\'PostTime\']; ?></time>
</div>
</div>
<div class="clear"></div>
</div><!-- end first row-->
<?php } //end if ?>
<?php
if($i == 2 ){
?>
<div class="div">
<div class="rst-col rst-col-25">
<div class="rst-postpic">
<a href="<?php echo $posts[$i][\'LinkPost\']; ?>"><img src="<?php echo $posts[$i+1][\'link\']; ?>" alt="" style="height: 385px;width: 770px"/></a>
<?php echo $i+1; ?>
</div>
<div class="rst-postinfo">
<a href="#"><span>Sport</span></a>
<h6><a href="<?php echo $posts[$i][\'LinkPost\']; ?>"><?php echo $posts[$i][\'Title\']; ?></a></h6>
<time><i class="fa fa-clock-o"></i><?php echo $posts[$i][\'PostTime\']; ?></time>
</div>
</div>
<div class="rst-col rst-col-25">
<div class="rst-postpic rst-postvideo">
<a href="<?php echo $posts[$i+1][\'LinkPost\']; ?>"><img src="<?php echo $posts[$i+2][\'link\']; ?>" alt="" style="height: 385px;width: 770px"/></a>
<?php echo $i+2; ?>
</div>
<div class="rst-postinfo">
<a href="#"><span>Sport</span></a>
<h6><a href="<?php echo $posts[$i+1][\'LinkPost\']; ?>"><?php echo $posts[$i+1][\'Title\']; ?></a></h6>
<time><i class="fa fa-clock-o"></i><?php echo $posts[$i+1][\'PostTime\']; ?></time>
</div>
</div>
<div class="rst-col rst-col-50">
<div class="rst-postpic">
<a href="<?php echo $posts[$i+2][\'LinkPost\']; ?>"><img src="<?php echo $posts[$i+3][\'link\']; ?>" alt="" style="height: 385px;width: 770px"/></a>
<?php echo $i+3; ?>
</div>
<div class="rst-postinfo">
<a href="#"><span>Sport</span></a>
<h6><a href="<?php echo $posts[$i+2][\'LinkPost\']; ?>"><?php echo $posts[$i+2][\'Title\']; ?></a></h6>
<time><i class="fa fa-clock-o"></i><?php echo $posts[$i+2][\'PostTime\']; ?></time>
</div>
</div>
<div class="clear"></div>
</div><!--end second row-->
<?php
}//end if
}//end for loop ?>
</div>
</div>
</div>
我面临的问题是,当wordpress有6篇帖子时,在这种情况下,我会增加loop-like的值
for($i=1;$i<count($posts);$i+=3)
当帖子数大于等于6篇小于或等于6篇帖子时,此问题将被删除,如下所示
当我有6篇帖子时,网格显示如下图所示,当帖子数为8时,网格填充不知道是什么问题
这个循环给了我唯一的值,但最后两列没有图像,但当我在wordpress中再上传两篇文章,并且数据库中的帖子总数=8时,就会出现图像,告诉我循环中的问题是什么