2017-05-03 49 views
0

我试图做一段时间,并显示内容在3列李。 到目前为止好,但我想条件只apear以字母A或B或C开头的单词如何做一个WHILE条件的子字段

的问题是,我只设法创建创建的状态,因此,代码正在创建空字段。 所以我的问题是如果有一种方法可以在中创建这个条件,而

我的代码 [更新] [更新为一个工作版本]

if(have_rows('insert_breed_name_to_add')){ 
$count = 0; 
while(have_rows('insert_breed_name_to_add')){ 

    the_row(); 

    if(get_sub_field('breed_name')[0] == 'A'){ 

      $count++; 
      // If it is first time create "col-4" 
      if($count == 1 || $count%3 == 1){ ?> 
      <div class="col-4"><ul> 
      <?php } ?> 

      <!-- Create Li --> 
      <li><?php the_sub_field('breed_name'); ?></li> 

      <!-- close col-4 --> 
      <?php if ($count%3 == 0){?> 
       </ul> 
      </div> 
      <?php } 
     } 
    } 
    } 

?> 
+0

首先,'$ count = 1;'在循环内被初始化,应该在外面吗? –

+0

谢谢@Antonis,我想是这样,否则它会把每个李都包在一个列中。我会尝试修改它。 – grcoder

回答

1

不知道你有什么做的,但看看下面:

<?php 

if(have_rows('insert_breed_name_to_add')){ 
    $count = 0; 
    while(have_rows('insert_breed_name_to_add')){ 

     the_row(); 

     // If it starts from A or B or C 
     if(get_sub_field('breed_name')[0] == 'A' || get_sub_field('breed_name')[0] == 'B' || get_sub_field('breed_name')[0] == 'C'){ 

       $count++; 
       // If it is first time create "col-4" 
       if($count == 1){ ?> 
       <div class="col-4"><ul> 
       <?php } ?> 

       <!-- Create Li --> 
       <li><?php the_sub_field('breed_name'); ?></li> 

       <!-- close col-4 --> 
       <?php if ($count%3 == 0){?> 
        </ul> 
       </div> 
       <?php } 


     } 
    } 
} 

?> 
+0

谢谢@Antonis!几乎是这样!我只是改变一点,按我想要的方式工作。这样它就产生3列3,当到达第3时,它沿着一条线并继续。完美:) – grcoder

1
<?php if(have_rows('insert_breed_name_to_add')): 
    while(have_rows('insert_breed_name_to_add') **MAYBE INSERT CONDITION HERE? BUT HOW?**): the_row(); 
while (have_rows('your_custom_field_values')): the_row(); 
    $count = 1; 
    if ($count%3 == 1) 
    {?> 
    <div class="col-4"><ul> 
    <?php } ?> 
    <? if(get_sub_field('breed_name')[0] == 'a' OR get_sub_field('breed_name')[0] == 'A'){ ?> 
    <li><?php the_sub_field('breed_name'); ?></li> 
     <?php }?> 
     <?php if ($count%3 == 0){?> 
     </ul></div> 
     <?php } 
     $count++; 
     if ($count%3 != 1)?> 
     </ul> 
     </div> 
     <?php endwhile; ?> 
     <?php endwhile; ?> 
     <?php endif; ?> 
+0

谢谢@Exprator 我设法通过这样做来改进代码。但是仍然在做错事。它得到的是以A开头的单词,但也有B单词。这很奇怪......你认为安东尼斯·齐莫多斯说过,计数应该出来吗?因为我尝试了,它破裂了。 – grcoder

+0

兄弟不是问题:) – Exprator

相关问题