【问题标题】:Display div every second loop [duplicate]每隔一个循环显示 div [重复]
【发布时间】:2014-01-07 07:54:51
【问题描述】:

我需要在我的 wordpress 循环中每隔一个项目添加一个 div。目前我正在使用下面的代码(有效),但我认为有一种更好/更清洁的方法可以做到这一点。我将如何更改下面的代码?任何关于在哪里看的建议都会很棒,因为这对我来说似乎很乱。提前致谢。

<?php while ( have_posts() ) : the_post(); ?>
<?php            
  $col_select = $data['example_select'];
  if ($col_select == '2 Col') {
    get_template_part( '/templates/article/content', 'standard' ); ?>
<?php if( $wp_query->current_post == '1') { ?>
  </div><div class="row">
<?php } if( $wp_query->current_post == '3') { ?>
  </div><div class="row">
<?php } if( $wp_query->current_post == '5') { ?>
  </div><div class="row">
<?php } if( $wp_query->current_post == '7') { ?>
  </div><div class="row">
<?php } if( $wp_query->current_post == '9') { ?>
  </div><div class="row">
<?php } ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    if( $wp_query-&gt;current_post % 2 != 0) 应该这样做。

    见:http://php.net/manual/en/language.operators.arithmetic.php

    编辑:当然,如果您删除了帖子,它会中断,但这是基于您当前的代码。该位所在的循环在哪里?你可以用计数器来做。

    【讨论】:

    • 哈哈,我就是按照你的写法写的!
    • 精彩。如此简单......当它让我杰西卡时我会接受
    【解决方案2】:

    均匀

    if($wp_query->current_post % 2)
    

    会为 %2 这样做,因为 1 = true 和 0 = false

    【讨论】:

    • 不过,如果我们决定将三样东西扔进列中,这就会中断,等等。
    【解决方案3】:

    您应该尽可能避免打开和关闭&lt;?php ... ?&gt; 标签。这会阻止阅读您的代码。其次,如果你想每隔一行结束你的div,然后使用模测试,如下所示:

    <?php 
      while ( have_posts() ) : the_post();
        $col_select = $data['example_select'];
        if ($col_select == '2 Col') {
          get_template_part( '/templates/article/content', 'standard' ); 
          if ( $wp_query->current_post % 2 == 1 ) {
            echo "</div><div class=\"row\">";
          }
        }
      endwhile;
    ?>
    

    但是,真正干净的方法是将显示推迟到您在此处呈现的模板。在不知道get_template_part 使用什么的情况下,很难说您将如何传递适当的数据(即查询的“下两个”部分。

    【讨论】:

      【解决方案4】:

      模数方法是正确的,但如果$wp_query-&gt;current_post 没有返回递增数字,则给出的选项会中断。

      最好的办法是创建另一个变量来存储您循环的次数,如下所示。

      <?php 
      $x = 1;
      while ( have_posts() ) : the_post();
          $col_select = $data['example_select'];
          if ($col_select == '2 Col')
          {
              get_template_part( '/templates/article/content', 'standard' );
              if(($x % 2) == 1) { ?>
                  </div><div class="row">
              <?php }
              $x++;
          }
      endwhile;
      

      以上意味着对于2 Col 匹配的每一秒结果,您将获得&lt;/div&gt;&lt;div class="row"&gt; 输出。

      【讨论】:

        【解决方案5】:
        <?php while ( have_posts() ) : the_post(); ?>
            <?php            
                $col_select = $data['example_select'];
            if ($col_select == '2 Col')
            {
                get_template_part( '/templates/article/content', 'standard' ); ?>
                <?php if(($wp_query->current_post % 2) == 1) { ?>
                    </div><div class="row">
                <?php } ?>
        

        current_post % 2 如果是奇数则返回 1。

        【讨论】:

        • @ollieread 不,它会为偶数返回 0。他是正确的,奇数是 1。
        • 不,不会。奇数除以 2 的余数是 1。如果是偶数,它将返回 0,就像下面 Jessica 的替代答案一样。
        • 抱歉,我的错,没有考虑。
        猜你喜欢
        • 2021-08-27
        • 2018-03-25
        • 2014-08-27
        • 2012-01-13
        • 2017-04-30
        • 2019-09-29
        • 1970-01-01
        • 2012-02-15
        • 2013-02-07
        相关资源
        最近更新 更多