在我的高级自定义字段中,我在一个组中有一个组。
内部群体中的字段是选择部分膳食。除了该组中的第一行外,格式将完全相同
我编写的代码输出食物名称和与每个食物名称相关的值(乘以另一个变量)。
除了输出它们之外,第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;
...除了第一行之外,我重复了很多相同的代码。 有没有办法循环浏览所有字段并应用一种格式,如果该字段是&#39; on_waking&#39;如果不是,还有另一种格式?
答案 0 :(得分:0)
尝试使用动态值循环进餐_:`
$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 } } ?>`
它可能有用!!
答案 1 :(得分:0)
我的解决方案是使用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...
}
}
}