【问题标题】:PHP Add every 2 items inside DIV in WordPress loopPHP在WordPress循环中的DIV中添加每2个项目
【发布时间】:2016-07-19 17:56:18
【问题描述】:

我需要 PHP 代码在 WordPress 循环中的 DIV 中添加每 2 个项目。

例如,我需要这样:

<div class="wrap">
    post
    post
 </div>

<div class="wrap">
    post
    post
 </div>

<div class="wrap">
    post
    post
 </div>

这是我的 wordpress 循环,但不工作,我需要在 DIV 中每 2 个帖子:

<?php if ( have_posts() ) : // If have post start. ?>


    <?php $i = 0; ?>


    <?php while ( have_posts() ) : the_post(); // Start Loop: ?>


        <?php if ( $i % 2 ==  0) : ?>
            <div class="wrap">
        <?php endif; ?>


        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php the_content(); ?>
        </article>


        <?php if ( $i % 2 == 0 ) : ?>
            </div>
        <?php endif; ?>


    <?php $i++; endwhile; // End Loop. ?>


<?php endif; // If have post end. ?>

谢谢。

【问题讨论】:

  • 如果您在主循环中,那么您可以使用以下内容进行检查:if( $wp_query-&gt;current_post % 2 == 0 ){...} 或者您可以在循环外设置计数器 $i=0; (if ( have_posts() ) : while ( have_posts() ) : the_post();),然后检查它是否为模数2 为 0:$if( i % 2 == 0) 并在每次迭代时递增 ($i++;),在 endwhile 之前。
  • 不工作@dingo_d
  • 显示无效的代码。
  • @dingo_d 阅读我的 wordpress 循环代码。
  • 这个link可以帮助你。

标签: php wordpress loops for-loop while-loop


【解决方案1】:

正确的循环应该是这样的:

 <?php if(have_posts()) : ?>
 <?php $no_of_posts = wp_count_posts()->publish; ?>
 <div class="wrap">
     <?php $i=1; while(have_posts()) : the_post(); ?>
     <div class="post">
         post text
     </div>
     <?php if($i%2==0 && $i!=$no_of_posts) : ?>
     </div>
     <div class="wrap">
     <?php endif; ?>
     <?php $i++; endwhile; ?>
 </div>
 <?php endif; ?>

【讨论】:

    【解决方案2】:

    我认为这应该可以完成工作:

    <div class="wrap">
    <?php
    $query = new WP_Query();
    if ( $query->have_posts() ):
      $i=0;
      while ( $query->have_posts() ) :
        $query->the_post();
        if($i%2==0 && $i<$query->post_count-1 && $i>0):
          echo '</div><div class="wrap">' 
        endif;
    ?>
        <!--html here-->
    <?php
        $i++;
      endwhile;
    endif;
    ?>
    </div>
    

    【讨论】:

    • 我需要默认循环,而不是自定义循环 wp_query
    【解决方案3】:

    问题是您在 $i 的偶数值上同时打印 &lt;div&gt; 和 &lt;/div&gt;。这就是为什么他们总是只包装一个而第二个帖子放在一边。

    您必须在偶数上回显&lt;div&gt;,在奇数上回显&lt;/div&gt;:

    <?php if ( have_posts() ) : // If have post start. ?>
    
        <?php $i = 0; ?>
    
        <?php while ( have_posts() ) : the_post(); // Start Loop: ?>
    
            <?php if ( $i % 2 ==  0) : ?>
                <div class="wrap">
            <?php endif; ?>
    
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php the_content(); ?>
            </article>
    
            <!-- changed == 0 to != 0  -->
            <?php if ( $i % 2 != 0 ) : ?>
                </div>
            <?php endif; ?>
    
        <?php $i++; endwhile; // End Loop. ?>
    
            <!-- added closing </div> for odd number of posts -->
            <?php if ( $i % 2 != 0 ) : ?>
                </div>
            <?php endif; ?>
    
    <?php endif; // If have post end. ?>
    

    我在循环之后添加了第二个&lt;/div&gt;,因为如果没有它,如果你有奇数个帖子,你根本不会得到结束标签。

    【讨论】:

    • 类似于我上面放的:D
    【解决方案4】:

    好的,根据 Aviram here 的回答,我创建了这个:

    $i = 1;
    $out = '';
    $endingNeeded = false;
    
    if ( have_posts() ) {
        while ( have_posts() ) {
    
            if ( $i % 2 == 1) {
                $out .= '<div class="wrap">';
                $endingNeeded = true;
            }
    
            the_post(); // Start Loop:
    
            $out .= '<article id="post-'. get_the_ID().'" class="'.implode(get_post_class(), ', ').'">
                    '.get_the_content().'
                </article>';
    
            if ( $i % 2 == 0 ) {
                $out .= '</div>';
                $endingNeeded = false;
            }
    
            $i++;
        }
    }
    
    if($endingNeeded) {
        $out .= '</div>';
    }
    
    echo $out;
    

    应该可以正常工作。

    【讨论】:

      【解决方案5】:

      你应该这样做:

          <?php 
          if ( have_posts() ) {
              $i=0;
              while ( have_posts() ) {
                 if($i%2==0):?>
               <div class="wrap">
         <?php     
          else : ?>
      </div>
      <?php 
        endif;       
        $i++;
      }
      }    
      ?>
      

      【讨论】:

        【解决方案6】:

        你是这个意思吗?

        <?php 
        if ( have_posts() ) {
            while ( have_posts() ) {
                for ($i=0; $i<2; $i++) {
                    // what do you like to do
                    ?>
                    <!-- HTML CODE -->
                    <?php
                }
            } 
        }else{
            // ...
        }
        ?>
        

        为什么不在 WordPress 循环中添加一个简单的 for 循环?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-23
          • 1970-01-01
          • 2012-02-15
          • 1970-01-01
          • 2017-07-28
          • 1970-01-01
          相关资源
          最近更新 更多