如何在某些POST设置后添加div块

时间:2015-07-30 作者:user32447

好的,我把我的帖子分为不同的风格设置。如下图所示。第一根柱子是一个大木块。接下来的四根柱子很小,与较大的柱子相邻。

enter image description here

我使用下面的循环来实现这种效果。

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<!--First Post -->
<?php elseif ($count <= 1) : ?>
<div class="style-1"><?php the_content(); ?></div>

<!--Next Four Boxes -->
<?php elseif ($count <= 5) : ?>
<div class="style-2"><?php the_content(); ?></div>

<!--Next Six Boxes -->
<?php elseif ($count <= 10) : ?>
<div class="style-3"><?php the_content(); ?></div>

<?php endif; ?>
<?php endwhile; ?>
<php else : ?>
<?php endif; ?>
现在,我的问题是。How do I break the loop to add a special div after the first five post and continue the loop after the special div? 就像下面的照片。灰色区域。我还想在接下来的六篇文章之后再添加一个特殊的div,每个特殊的div都有不同的内容。

enter image description here

1 个回复
最合适的回答,由SO网友:Alex Andrieș 整理而成

使用%设置模板(样式),也可以使用:n个子项(16n+1)。。。

<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php $st = 16; //after how many blocks you want to repeat the pattern 
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count%$st == 1) : ?>
<div class="style-1"><?php the_content(); ?></div>
<?php elseif ($count%$st == 2 || $count%$st == 3) : ?>
<div class="style-2"><?php the_content(); ?></div>
<?php elseif ($count%$st == 4 || $count%$st == 5 || $count%$st == 6) : ?>
<div class="style-3"><?php the_content(); ?></div>
<?php elseif ($count%$st == 7 || $count%$st == 8 || $count%$st == 9 || $count%$st == 10) : ?>
<div class="style-4"><?php the_content(); ?></div>
<?php elseif ($count%$st == 11 || $count%$st == 12 || $count%$st == 13 || $count%$st == 14 || $count%$st == 15 ) : ?>
<div class="style-5"><?php the_content(); ?></div>
<?php elseif ($count%$st == 0 ) : ?>
<div class="style-6"><?php the_content(); ?></div>
<?php endif; ?>
<?php endwhile; ?>
或者(我不确定我是否理解您的需要)

<?php if($count % $st == 0){?>
// do your code
<?php }?> 
//continue with your loop code
尝试此代码

    <?php if (have_posts()) :
    $count = 0;
    while (have_posts()) : the_post();
        $count++;
        if ($count  == 1) : ?> <!--first box -->
            <div class="style-1"><?php the_content(); ?>
        <?php elseif ($count  > 1 && $count  <5) : ?></div> <!--next three boxes -->
            <div class="style-2"><?php the_content(); ?></div>
        <?php elseif ($count  == 5) : ?> <!-- box five and break div -->
            <div class="style-2"><?php the_content(); ?></div>
            <div class="first-break-div"><?php /* the content from first break div */ ?></div>
        <?php elseif ($count >5 && $count < 11) : ?> <!--next five boxes -->
            <div class="style-3"><?php the_content(); ?></div>
        <?php elseif ($count == 12) : ?> <!--last box and last break div -->
            <div class="style-3"><?php the_content(); ?></div>
            <div class="last-break-div"><?php /* the content from last break div */ ?></div>
        <?php endif;
    endwhile;
endif; ?>
这是我的输出

<div class="style-1"></div>
<div class="style-2"></div>
<div class="style-2"></div>
<div class="style-2"></div>
<div class="style-2"></div>
<div class="first-break-div"></div>
<div class="style-3"></div>
<div class="style-3"></div>
<div class="style-3"></div>
<div class="style-3"></div>
<div class="style-3"></div>
<div class="style-3"></div>
<div class="last-break-div"></div>

结束