在wordpress中每四个帖子后添加一个行div

时间:2015-04-28 14:53:21

标签: php wordpress

我想在我的wordpress网站上每四个帖子后添加一个0x7f。以下代码用于在我的页面上生成帖子类型:

div class="row"

2 个答案:

答案 0 :(得分:0)

创建变量,在每个循环步骤中迭代它并检查% 4 == 0;

$i = 0;

while (...) {
    echo $i % 4 == 0 ? 'row' : ''; // echo .row in first step too. If you want in the 4th step, just change the result of modulo operator

    // your code here

    $i++;
}

答案 1 :(得分:0)

以下应该做你想做的事

<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=8&post_type=branding' . '&paged=' . $paged);
?>
<div class="row">
<?php
$i = 0;
while ($wp_query->have_posts()):
    $wp_query->the_post();
    if ($i == 4) {
        $i = 0;
        ?>
        </div>
        <div class="row">
        <?php
    }
    ?>

    <div class="col-md-3">
        <div class="featured-project-image">
            <?php
            if (has_post_thumbnail()) {

                // check if the post has a Post Thumbnail assigned to it.
                the_post_thumbnail();
            } 
            else {
                echo '<img src="';
                echo get_bloginfo('stylesheet_directory');
                echo '/images/placeholder.jpg"/>';
            } ?>
            </div>
        </div>
    <?php
    $i++;
endwhile; ?>
</div>

在while循环之前启动div可确保前4行也包含在一行中。