基于循环打开和关闭div

时间:2018-04-17 00:07:06

标签: php html wordpress loops count

我知道也许这不是一个与WP相关的问题,但我认为这里的错误是循环而我错过了一些东西。 我正在尝试创建一个自定义的“存档”页面,使用循环,我想在4组中设置我的帖子的样式。 现在我在这个类别中有10个帖子,我认为它们不会更多,但我不能强迫任何东西因为它们可以轻易地增加或减少。

<div id="bun" class="bun sezmenu">
<?php $catquery = new WP_Query( 'category_name=bun&&posts_per_page=-1&&nopaging=true'); $et_npb = 1;$id = 37;$category = get_category($id);$count = $category->category_count;?>
<?php if ($catquery->have_posts() ) : while($catquery->have_posts()) : $catquery->the_post();?>
    <?php if ($et_npb === 1 || $et_npb % 5 === 0 ){ echo '<div class="row">' ;};?>
    <div class="col">
        <div class="list__item list__item_ajax">
            <a href="" title="<?php the_title();?>" class="umodal__open" umodal-src="<?php the_permalink();?>">
                <img class="lazyload rounded-circle"
                     src="<?php echo get_stylesheet_directory_uri(); ?>/img/bn/bun.png"
                     data-src="<?php echo get_stylesheet_directory_uri(); ?>/img/bun.png"
                     height="128" width="128"
                     alt="<?php the_title();?>" />
            </a>
        </div><!-- list__item list__item_ajax -->
    </div>
<?php ++$et_npb; if ($et_npb % 5 == 0 || $et_npb == $count){echo '</div>';};
endwhile;endif;?>

正如您在下图中看到的那样,出现了问题,我无法弄清楚是什么。 我是否正确使用该功能来计算此类别中的所有帖子?

First row is correct (4 elements), second row is wrong (5 elements)

我还尝试使用此工具来尝试了解div是打开还是关闭。

<divid="bun" class="bun sezmenu">
  |<divclass="row">
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |</div>
  |<divclass="row">
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |</div>
  |<divclass="row">
  |       |<divclass="col">
  |       |       |<divclass="list__item list__item_ajax">
  |       |       |</div>
  |       |</div>
  |</div>
  |<divid="piadina" class="piadina sezmenu">
  |</div>
  |<divid="crepe" class="crepe sezmenu">
  |</div>
  |<divid="dolci" class="dolci sezmenu">
  |</div>
  |<divumodal-id="umodal-1" class="umodal">
  |       |<divclass="umodal__inner">
  |       |       |<divclass="umodal__content">
  |       |       |</div>
  |       |</div>
  |</div>

它说我遗漏了1个标签,但这很明显,因为我的循环没有正确关闭所有div。

这是它应该如何工作,但我无法弄清楚,它不能保持'硬编码':

<div id="bun" class="bun sezmenu">
<?php $catquery = new WP_Query( 'category_name=bun&&posts_per_page=-1&&nopaging=true'); $et_npb = 1;$id = 37;$category = get_category($id);$count = $category->category_count;?>
<?php if ($catquery->have_posts() ) : while($catquery->have_posts()) : $catquery->the_post();?>
<?php if ($et_npb === 1 || $et_npb === 5 || $et_npb ===9){;?><div class="row"><?php };?>
    <div class="col">
        <div class="list__item list__item_ajax">
            <a href="" title="<?php the_title();?>" class="umodal__open" umodal-src="<?php the_permalink();?>">
                <img class="lazyload rounded-circle"
                     src="<?php echo get_stylesheet_directory_uri(); ?>/img/bn/bun.png"
                     data-src="<?php echo get_stylesheet_directory_uri(); ?>/img/bun.png"
                     height="128" width="128"
                     alt="<?php the_title();?>" />
            </a>
        </div><!-- list__item list__item_ajax -->
    </div>
    <?php ++$et_npb; if ($et_npb === 5 || $et_npb === 9 ||  $et_npb === $count+1){echo '</div>';};
        endwhile;endif;?>
</div><!-- .sezmenu -->

This is what I would like to achieve

1 个答案:

答案 0 :(得分:0)

有时最简单的解决方案根本无法实现,但这一次,我找到了我的! 你怎么了?可以改进吗?

<div id="bun" class="bun sezmenu">
<?php $catquery = new WP_Query( 'category_name=bun&&posts_per_page=-1&&nopaging=true'); $et_npb = 0;$id = 37;$category = get_category($id);$count = $category->category_count;?>
<?php if ($catquery->have_posts() ) : while($catquery->have_posts()) : $catquery->the_post();?>
<?php if (($et_npb === 0) || ($et_npb %4 == 0)){;?><div class="row"><?php };?>
    <div class="col">
        <div class="list__item list__item_ajax">
            <a href="" title="<?php the_title();?>" class="umodal__open" umodal-src="<?php the_permalink();?>">
                <img class="lazyload rounded-circle"
                     src="<?php echo get_stylesheet_directory_uri(); ?>/img/bn/bun.png"
                     data-src="<?php echo get_stylesheet_directory_uri(); ?>/img/bun.png"
                     height="128" width="128"
                     alt="<?php the_title();?>" />
            </a>
        </div><!-- list__item list__item_ajax -->
    </div>
    <?php ++$et_npb; if ( ($et_npb %4 == 0) ||  ($et_npb === $count)){echo '</div>';};
        endwhile;endif;?>
</div><!-- .sezmenu -->