Arrays 自定义字段-转发器下拉列表-仅列出唯一字段&;非空值
下面是我正在使用ACF中继器子字段(firma)来存储一些数据。通常,用户在这里输入其母公司名称,如:谷歌、阿里巴巴、索迪斯等。因此,在多个帖子中,此字段的值可能是相同的。目前我有以下代码:Arrays 自定义字段-转发器下拉列表-仅列出唯一字段&;非空值,arrays,wordpress,advanced-custom-fields,Arrays,Wordpress,Advanced Custom Fields,下面是我正在使用ACF中继器子字段(firma)来存储一些数据。通常,用户在这里输入其母公司名称,如:谷歌、阿里巴巴、索迪斯等。因此,在多个帖子中,此字段的值可能是相同的。目前我有以下代码: $args = array( 'post_type' => 'opencourses', 'meta_key' => 'terminy_warsztatow' ); $the_query = new WP_Query($args);
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;
但它却没有显示任何值。我假设“firma”是repeater中的简单文本输入类型。如果是这样,那么arrya_unique函数将无法用于字符串输出
您需要将每个值保存在数组中,然后使用in_数组函数使其唯一
请参阅下面的代码。
$args=array(
“post_type”=>“opencourses”,
“meta_key”=>“terminy_warsztatow”
);
$thew_query=newwp_query($args);
如果($the\u query->have\u posts()):
回声';
while($the_query->have_posts()):$the_query->the_post();
如果(有行('terminy\u warsztatow')):
//$PreValue=array();
while(have_rows('terminy_warsztatow')):the_row();
//显示子字段
$filter\u var=get\u sub\u字段('firma');
//比较保存数组中的当前值
if(!in_数组($filter_var,$PreValue))
{
回声';
echo$filter_var;
回声';
}
//在数组中保存值
$PreValue[]=$filter\u var;
结束时;
其他:
//找不到行
endif;
结束时;
回声';
endif;
希望这对你有帮助!
享受
$filtr_var = get_sub_field('firma');
$result = array_unique($filtr_var);
echo $result;
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
echo '<select type="text" class="form-control" name="filtr_lokalizacja">';
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
// $PreValue = array();
while (have_rows('terminy_warsztatow')) : the_row();
// display your sub fields
$filtr_var = get_sub_field('firma');
// compare current value in saved array
if( !in_array( $filtr_var, $PreValue ) )
{
echo '<option value="'. $filtr_var .'">';
echo $filtr_var;
echo '</option>';
}
// save value in array
$PreValue[] = $filtr_var;
endwhile;
else :
// no rows found
endif;
endwhile;
echo '</select>';
endif;