メインループでループ数の取得
-
<div id="test1"> </div> <div id="test2"> </div> <div id="test3"> </div> <div id="test4"> </div> <div id="test5"> </div>
上記のdivグループが5個あって、2個置きにdivでさらに囲いたいです。
wordpressで下記のように書いたのですが、divの数が奇数の場合、divが閉じタグが入りません。
ループ数の最後の数が分かれば判定する事は可能なのですが、取得方法が分かりません。
<?php count($post): ?>では1なので判定材料に使えませんでした。<?php $content_count=0; ?> <?php if(have_posts()): // 記事が存在する場合?> <?php while(have_posts()): the_post(); ?> <?php if(($content_count % 2) === 0) : ?> <div class="add"> <?php endif; ?> <div id="test<?php echo $content_count+1; ?>"> </div> <?php if(($content_count % 2) === 1) : ?> </div> <?php endif; ?> <?php $content_count++; ?> <?php endwhile; wp_reset_postdata(); // ループ終了&取得データをリセット?>
2件の返信を表示中 - 1 - 2件目 (全2件中)
2件の返信を表示中 - 1 - 2件目 (全2件中)
- トピック「メインループでループ数の取得」には新たに返信することはできません。