WPquery +每第n个元素添加一行

时间:2017-10-28 11:47:56

标签: wordpress twitter-bootstrap counter

所以如果遵循以下情况 - 我在下面的代码中得到了一个wpquery。

<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
        <div class="container">
            <div class="row">
                <div class="col-sm-12">
                    <?php
                        $args = array(
                        'post_type'      => 'page',
                        'posts_per_page' => -1,
                        'post_parent'    => $post->ID,
                        'order'          => 'ASC',
                        'orderby'        => 'menu_order'
                     );


                    $parent = new WP_Query( $args );

                    if ( $parent->have_posts() ) : ?>

                        <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>

                        <div class="row">
                            <div class="col-sm-12 col-lg-3">
                                <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
                            </div>
                        </div>
                        <?php endwhile; ?>

                        <?php endif; wp_reset_query(); ?>
                </div>
            </div>
        </div>
    </section>

我想要实现的是让循环像这样工作:

<ROW>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
<COL-LG-3>
</ROW>
事实上,我想要实现的是在行内部有4个元素而不创建不同的循环。我知道我应该使用一些计数器,但我不知道如何; /

感谢

1 个答案:

答案 0 :(得分:1)

在4列后添加新行

<section class="row service_block_row bgf" id="page-<?php the_ID(); ?>">
        <div class="container">
            <div class="row">
                <div class="col-sm-12">
                    <?php
                        $args = array(
                        'post_type'      => 'page',
                        'posts_per_page' => -1,
                        'post_parent'    => $post->ID,
                        'order'          => 'ASC',
                        'orderby'        => 'menu_order'
                     );


                    $parent = new WP_Query( $args );

                    if ( $parent->have_posts() ) : 
                     $count=0;
                    ?>
                        <div class="row">
                        <?php while ( $parent->have_posts() ) : $parent->the_post(); 
                            $count++;
                        ?>
                            <div class="col-sm-12 col-lg-3">
                                <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
                            </div>

                        <?php 
                        if($count%4==0)
                        {
                            echo '</div><div class="row">';
                        }
                        endwhile; ?>
                        </div>
                        <?php endif; ?>

                        <?php wp_reset_query(); ?>
                </div>
            </div>
        </div>
相关问题