Цикл по всем строкам группы ACF без указания имени строки - PullRequest
0 голосов
/ 23 мая 2018

В моих дополнительных настраиваемых полях у меня есть группа внутри группы.

Поля во внутренней группе предназначены для выбора частей блюд.Помимо первой строки в этой группе, формат будет точно таким же

enter image description here

Я написал код, который выводит название продукта и значениеассоциируется с каждым (умножается на другую переменную).

Помимо вывода их, в 5-й и 6-й строке есть оператор if.Они показывают, только если другая переменная $ feeds_per_day так же высока, как и номер еды.

Вот код ...

if( have_rows('zero') ):


?><div class="grid-50" style="float: left;"><table><tr><th colspan="2">Zero</th></tr><?php

while( have_rows('zero') ) : the_row(); ?>

        <tr><td>Upon Waking</td><td><?php the_sub_field('upon_waking')?></td></tr>

        <?php 
    if ( have_rows('meal_1') ){ 
        while( have_rows('meal_1') ) {
        the_row();

        $protein_object = (get_sub_field_object('protein_select'));                                 
        $protein_value = get_sub_field('protein_select');                                   
        $protein_label = ($protein_object['choices'][$protein_value]);

        $carb_object = (get_sub_field_object('carb_select'));                                   
        $carb_value = get_sub_field('carb_select');                                 
        $carb_label = ($carb_object['choices'][$carb_value]);

        $fat_object = (get_sub_field_object('fat_select'));                                 
        $fat_value = get_sub_field('fat_select');                                   
        $fat_label = ($fat_object['choices'][$fat_value]);

        $protein_amount = round($zero_protein_feed / $protein_value * 10) *10;
        $fat_amount = round($zero_fat_feed / $fat_value * 10) *10 ;

        ?><tr><td>Meal 1</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php }  }

        if ( have_rows('meal_2') ){ 
        while( have_rows('meal_2') ) {
        the_row();

        $protein_object = (get_sub_field_object('protein_select'));                                 
        $protein_value = get_sub_field('protein_select');                                   
        $protein_label = ($protein_object['choices'][$protein_value]);

        $carb_object = (get_sub_field_object('carb_select'));                                   
        $carb_value = get_sub_field('carb_select');                                 
        $carb_label = ($carb_object['choices'][$carb_value]);

        $fat_object = (get_sub_field_object('fat_select'));                                 
        $fat_value = get_sub_field('fat_select');                                   
        $fat_label = ($fat_object['choices'][$fat_value]);

        $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
        $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;

        ?><tr><td>Meal 2</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }

        if ( have_rows('meal_3') ){ 
        while( have_rows('meal_3') ) {
        the_row();

        $protein_object = (get_sub_field_object('protein_select'));                                 
        $protein_value = get_sub_field('protein_select');                                   
        $protein_label = ($protein_object['choices'][$protein_value]);

        $carb_object = (get_sub_field_object('carb_select'));                                   
        $carb_value = get_sub_field('carb_select');                                 
        $carb_label = ($carb_object['choices'][$carb_value]);

        $fat_object = (get_sub_field_object('fat_select'));                                 
        $fat_value = get_sub_field('fat_select');                                   
        $fat_label = ($fat_object['choices'][$fat_value]);

        $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
        $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;

        ?><tr><td>Meal 3</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }

        if ( have_rows('meal_4') ){ 
        while( have_rows('meal_4') ) {
        the_row();

        $protein_object = (get_sub_field_object('protein_select'));                                 
        $protein_value = get_sub_field('protein_select');                                   
        $protein_label = ($protein_object['choices'][$protein_value]);

        $carb_object = (get_sub_field_object('carb_select'));                                   
        $carb_value = get_sub_field('carb_select');                                 
        $carb_label = ($carb_object['choices'][$carb_value]);

        $fat_object = (get_sub_field_object('fat_select'));                                 
        $fat_value = get_sub_field('fat_select');                                   
        $fat_label = ($fat_object['choices'][$fat_value]);

        $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
        $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;

        ?><tr><td>Meal 4</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }

        if ( have_rows('meal_5') ){ 
        while( have_rows('meal_5') ) {
        the_row();

        $protein_object = (get_sub_field_object('protein_select'));                                 
        $protein_value = get_sub_field('protein_select');                                   
        $protein_label = ($protein_object['choices'][$protein_value]);

        $carb_object = (get_sub_field_object('carb_select'));                                   
        $carb_value = get_sub_field('carb_select');                                 
        $carb_label = ($carb_object['choices'][$carb_value]);

        $fat_object = (get_sub_field_object('fat_select'));                                 
        $fat_value = get_sub_field('fat_select');                                   
        $fat_label = ($fat_object['choices'][$fat_value]);

        $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
        $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;

            if ($feeds_per_day > 4){

        ?><tr><td>Meal 5</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }

            }

        if ( have_rows('meal_6') ){ 
        while( have_rows('meal_6') ) {
        the_row();

        $protein_object = (get_sub_field_object('protein_select'));                                 
        $protein_value = get_sub_field('protein_select');                                   
        $protein_label = ($protein_object['choices'][$protein_value]);

        $carb_object = (get_sub_field_object('carb_select'));                                   
        $carb_value = get_sub_field('carb_select');                                 
        $carb_label = ($carb_object['choices'][$carb_value]);

        $fat_object = (get_sub_field_object('fat_select'));                                 
        $fat_value = get_sub_field('fat_select');                                   
        $fat_label = ($fat_object['choices'][$fat_value]);

        $protein_amount = round($zero_protein_feed / $protein_value * 10) * 10 ;
        $fat_amount = round($zero_fat_feed / $fat_value * 10) * 10 ;

            if ($feeds_per_day > 5){

        ?><tr><td>Meal 6</td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php } }

            }


endwhile;

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

... кроме первой строки, яповторяя много одного и того же кода. Есть ли способ циклически проходить по всем полям и применять один формат, если поле имеет значение "Upon_Waking", и другой формат, если нет?

Ответы [ 2 ]

0 голосов
/ 29 мая 2018

Решением для меня было использование цикла for ..

for ($i=1; $i<$feeds_per_day + 1; $i++) {
   if (have_rows('meal_'.$i)) {
     while (have_rows('meal_'.$i)) {

  the_row();
  //etc...

}}}

... в качестве альтернативы, если мои внутренние строки не имеют последовательных имен, я могу поместить их все в массив и пройти через нихкак это ...

$repeaters = array('zero', 'low', 'medium', 'high');
foreach ($repeaters as $repeater) {
if (have_rows($repeater)) {
while (have_rows($repeater)) {

  the_row();
  // etc...

    }
  }
}
0 голосов
/ 23 мая 2018

попробуйте это, используя динамические значения для цикла еды: `

    $mcount = 0; 
        while( have_rows('zero') ) : the_row(); $mcount++;?>

    <tr><td>Upon Waking</td><td><?php the_sub_field('upon_waking')?></td></tr>

    <?php
    if ( have_rows('meal_'.$mcount) ){ 
    while( have_rows('meal_'.$mcount) ) {
    the_row();

    $protein_object = (get_sub_field_object('protein_select'));                                 
    $protein_value = get_sub_field('protein_select');                                   
    $protein_label = ($protein_object['choices'][$protein_value]);

    $carb_object = (get_sub_field_object('carb_select'));                                   
    $carb_value = get_sub_field('carb_select');                                 
    $carb_label = ($carb_object['choices'][$carb_value]);

    $fat_object = (get_sub_field_object('fat_select'));                                 
    $fat_value = get_sub_field('fat_select');                                   
    $fat_label = ($fat_object['choices'][$fat_value]);

    $protein_amount = round($zero_protein_feed / $protein_value * 10) *10;
    $fat_amount = round($zero_fat_feed / $fat_value * 10) *10 ;

    ?><tr><td>Meal <?php echo$mcount; ?></td><td><?php echo $protein_amount ?>g <?php echo $protein_label ?>, <?php echo $fat_amount ?>g <?php echo $fat_label ?></td></tr><?php }  } ?>`

Это может сработать !!

...