2016-10-01 40 views
1

這是我的代碼,我想在第二個孩子循環中添加一個類。請告訴我,我該怎麼做。我是wordpress新手。如何添加一個類在特定的孩子在wordpress循環

<?php 
     $args = array(
      'posts_per_page' => 3, 
      'post_type' => 'hosting_plan', 
      'order' => 'ASC' 
     ); 
     query_posts($args); 
     if (have_posts()) : while (have_posts()) : the_post(); 
    ?> 
     <div class="hostplbx /*here i want to add class on second child*/"> 
      <h3><?php the_field('plan_name'); ?></h3> 
      <div class="hostprice"> 
       <span class="hosprice"><b class="rs"><?php the_field('plan_price'); ?></b> per month</span> 
       <span class="plandetail"><?php the_field('tag_line'); ?></span> 
      </div> 

      <?php the_content(); ?> 

      </div> 
      <?php endwhile; ?> 
      <?php endif; ?> 

回答

0

你可以使用一個簡單的迭代變量 - 一些示例代碼,你可以改變,以適用於您的模板:

$counter = 1; 

if (have_posts()) : while (have_posts()) : the_post(); 

    if($counter == 2) echo "<div class='second_child_class'>content goes here</div>"; 
    else echo "<div>content goes here</div>"; 

    $counter++; 

endwhile; endif; 
相關問題