将ACF子字段值显示为下拉列表

时间:2017-09-07 14:19:24

标签: php wordpress advanced-custom-fields

我正在使用ACF Pro,我有一个名为团队成员的自定义帖子类型。我有一个名为team_information的转发器字段,它有一个子字段“member_name” 如何在下拉列表中显示成员名称的所有值。现在它只显示默认值。任何帮助将不胜感激。

<?php
    // check if the repeater field has rows of data
    if( have_rows('team_information') ):
        // loop through the rows of data
        while ( have_rows('team_information') ) : the_row(); 
            $teamMember= get_sub_field('member_name'); 
        endwhile;
    endif;
?>

<select>
    <option value="name" selected><b>Select a Team Member</b></option>
    <?php foreach($teamMember as $team) :?>         
    <option><?php echo $team; ?></option>
</select>

1 个答案:

答案 0 :(得分:1)

尝试以下代码

<select>
<option value="name" selected><b>Select a Team Member</b></option>
<?php
      // check if the repeater field has rows of data
if( have_rows('team_information') ):
// loop through the rows of data
while ( have_rows('team_information') ) : the_row(); 
?>        

   <option><?php echo get_sub_field('member_name');  ?></option>
<?php
endwhile;
endif;
?>
</select>

如果您希望坚持使用您的代码,那么这就是您的解决方案

<?php
  $teamMember = array();
  // check if the repeater field has rows of data
  if( have_rows('team_information') ):
  // loop through the rows of data
  while ( have_rows('team_information') ) : the_row(); 

  $teamMember[]= get_sub_field('member_name'); 

 endwhile;
endif;
?>



<select>
  <option value="name" selected><b>Select a Team Member</b></option>
  <?php foreach($teamMember as $team) {?>          
  <option><?php echo $team; ?></option>
  <?php } ?>
</select>