PHP在WordPress循环中添加DIV中的每2个项目

时间:2016-03-31 10:23:37

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

我需要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. ?>

感谢。

6 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

问题是您在偶数<div>的值上打印</div>$i。这就是为什么他们总是只包装一个而第二个帖子站在一边。

您必须回复偶数上的<div>和奇数上的</div>

<?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. ?>

我在循环之后添加了第二个</div>,因为没有它,如果您的帖子数量奇数,则根本不会得到结束标记。

答案 2 :(得分:0)

你应该这样做:

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

答案 3 :(得分:0)

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

{
    "TEST_ID": "INV_CRE_184",
    "TEST_DESCRIPTION": "This validate Invoice Creation by mocking dependency",
    "TEST_PLAN": "LINK",
    "TEST_VARIABLE": [{
        "retailcountryCode": "DE"
    }],
    "TEST_CASE": [{
        "OUTPUT_PLACEHOLDER": "V1",
        "ACTION": "Generate",
        "PARAMETERS": [
            "GenerateVendor",
            "VENDOR",
            "${retailcountryCode}"
        ]
    }, {
        "OUTPUT_PLACEHOLDER": "P1",
        "ACTION": "Generate",
        "PARAMETERS": [
            "GeneratePayee",
            "${V1}",
            "${ofaOrg}"
        ]
    }, 

应该可以正常工作。

答案 4 :(得分:0)

正确的循环应如下所示:

 <?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; ?>

答案 5 :(得分:-1)

你这意味着什么?

<?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循环?