2012-07-25 67 views
0

我有一段代碼可以生成列表項。有沒有一種方法可以讓列表項有一個類,並且每次創建列表項時,類號都可以增加一個?增加類的變量

例如,該類將是「listitem1」,然後是第二個「listitem2」等,以便在創建每個列表項後,變量號增加1.下面是我正在使用的代碼:

<ul id="servicelist" class="clearfix"> 
     <?php if(get_field('homepage_service')): ?> 
      <?php while(the_repeater_field('homepage_service')): ?> 
       <li class="listitem"><img src="<?php the_sub_field('service_image'); ?>" width="144" height="103" /></li> 
      <?php endwhile; ?> 
     <?php endif; ?> 
     </ul> 

我可以在Flash(as3)中做到這一點,但我不確定它在PHP中如何工作。由於

回答

3

您需要使用PHP variables

<?php $count = 0; ?> 
<ul id="servicelist" class="clearfix"> 
    <?php if(get_field('homepage_service')): ?> 
     <?php while(the_repeater_field('homepage_service')): ?> 
      <?php $count++; ?> 
      <li class="listitem<?php echo $count; ?>"><img src="<?php the_sub_field('service_image'); ?>" width="144" height="103" /></li> 
     <?php endwhile; ?> 
    <?php endif; ?> 
</ul> 
+0

非常感謝你,那是一樣一樣的,因爲我已經習慣了 – Phill 2012-07-25 11:31:32

0

只需使用一個簡單的計數器 -

<ul id="servicelist" class="clearfix"> 
    <?php if(get_field('homepage_service')): 
     $count = 1; 
    ?> 
     <?php while(the_repeater_field('homepage_service')): ?> 
      <li class="listitem<?php echo $count ?>"><img src="<?php the_sub_field('service_image'); ?>" width="144" height="103" /></li> 
      $count++; 
     <?php endwhile; ?> 
    <?php endif; ?> 
    </ul> 
+0

的方式@本只是打敗了我 – DaveP 2012-07-25 10:28:33