Query_posts将每6个帖子包装在一起

时间:2015-06-19 14:58:55

标签: php wordpress custom-post-type

我使用query_post来调用我创建的名为'合作伙伴的自定义帖子类型中的所有帖子。

我希望如何将帖子包含在6个组中的div中。例如:

<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(); ?> 

但是我收到以下错误:意外的T_ENDWHILE

2 个答案:

答案 0 :(得分:0)

试试这段代码          <?php

            query_posts('post_type=partners');

            while ( have_posts() ) : the_post();

            //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 endif; ?>
            <?php endwhile; ?>

            <?php wp_reset_query(); ?> 

答案 1 :(得分:0)

我设法根据之前的example重建代码。我修改了我的代码以使用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(); ?>