Php 查询_帖子将每6篇帖子包装在一起
我使用query_post调用我创建的名为“partners”的自定义post类型中的所有post 我想要的是将帖子分成6组,以一个div的形式包装起来。例如:Php 查询_帖子将每6篇帖子包装在一起,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我使用query_post调用我创建的名为“partners”的自定义post类型中的所有post 我想要的是将帖子分成6组,以一个div的形式包装起来。例如: <div class="item"> <img src="#"/> <img src="#"/> <img src="#"/> <img src="#"/> <img src="#"/> <img src="#"/>
<div class="item">
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
</div>
<!-- 6 images/posts wrapped -->
<div class="item">
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
<img src="#"/>
</div>
<!-- 6 images/posts wrapped -->
这是我目前的代码:
<?php
query_posts('post_type=partners');
if (have_posts()) : while (have_posts()) ;
$posts = the_post();
if( $posts ): ?>
<? $lastIndex = count($posts) - 1; ?>
<? foreach($posts as $index => $post) : ?>
<? setup_postdata($post); ?>
<? if($index % 6 === 0) { ?>
<div class="item <?=$index === 0 ? 'active' : '' ?>">
<? } ?>
<div class="car-part-logo">
<? the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<? if(($index + 1) % 6 === 0 || $index === $lastIndex) { ?>
</div>
<? } ?>
<? endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
试试这个代码
我成功地根据前面的代码重建了代码。我将代码改为处理6项而不是3项
<?php query_posts('post_type=partners'); ?>
<?php $variable=0;?>
<div class="item active">
<?php while ( have_posts() ) : the_post(); ?>
<?php if(($variable+1)<7){ ?>
<div class="car-part-logo">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<?php $variable+=1; ?>
<?php }else{ ?>
<?php $variable=1; ?>
</div>
<div class="item">
<div class="car-part-logo">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<?php }?>
<?php endwhile; ?>
</div>
<?php wp_reset_query(); ?>
if($posts)有一个冗余的if代码>在第6行,未在后面结束。
<?php query_posts('post_type=partners'); ?>
<?php $variable=0;?>
<div class="item active">
<?php while ( have_posts() ) : the_post(); ?>
<?php if(($variable+1)<7){ ?>
<div class="car-part-logo">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<?php $variable+=1; ?>
<?php }else{ ?>
<?php $variable=1; ?>
</div>
<div class="item">
<div class="car-part-logo">
<?php the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
</div>
<?php }?>
<?php endwhile; ?>
</div>
<?php wp_reset_query(); ?>