如何在子场条件下做WHILE

时间:2017-05-03 11:08:44

标签: php wordpress while-loop advanced-custom-fields

我试图做一段时间并在3列li中显示内容。 到目前为止一切都那么好,但我想要只考虑以字母A,或B或C开头的单词。

问题是我只设法创建条件来创建 li ,因此,代码创建了空字段。 所以我的问题是,如果有办法在创建此条件。

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

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 }
        }
     }
  }

?>

2 个答案:

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

答案 1 :(得分:1)

不确定你想做什么,但请看下面的内容:

t=# with e as (
with c as (
  SELECT distinct colid
   , case when coldate < now() - '3 month'::interval then null else sum(colvalue) over (partition by date_trunc('month',coldate),colid) end summ
  , case when coldate < now() - '3 month'::interval then null else replace(substr(date_trunc('month',coldate)::text,1,7),'-','') end m
  FROM Cal_3Month
order by 1
)
select
  colid, hstore(m,summ::text)
from c
)
select colid,string_agg(hstore::text,',') from e group by colid order by 1 ;
 colid |           string_agg
-------+---------------------------------
     1 | "201702"=>"550","201703"=>"238"
     2 | "201703"=>"78","201704"=>"523"
     3 | "201704"=>"990"
     4 | "201703"=>"890"
     5 | "201702"=>"1000"
     6 |
     7 | "201702"=>"780"
     8 | "201702"=>"90"
(8 rows)