Home › Forums › Add-ons › Repeater Field › Display repeater subfield as a dropdown (label + value)
Hello,
I’ve noticed that it’s a quite popular topic yet, unfortunately – none of the found solutions are not working for me.
What I’m trying to achieve is to simply show a dropdown with labels and values based on a repeater subfield. I’ve managed to create such a piece of code:
function fnc_wybierz_wojewodztwo() {
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()): ?>
<select type="text" class="form-control filter-select" name="lokalizacja" id="lokalizacja"> <?php
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
while (have_rows('terminy_warsztatow')) : the_row();
$field = get_sub_field_object('wojewodztwo');
if( $field['choices'] ): ?>
<?php foreach( $field['choices'] as $value => $label ): ?>
<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
<?php endforeach;
endif;
endwhile;
else :
echo 'sorry, we didnt find anything'; // no rows found
endif;
endwhile; ?>
</select>
<?php endif; wp_reset_postdata();
}
add_shortcode( 'wybierz_wojewodztwo', 'fnc_wybierz_wojewodztwo' );
I struggle only with one thing – the dropdown provides endless/infinite number of options. I have no clue where the issue within code is. I’ve reset the loop, what else i can do?
If you are trying to create a select field that matches the choices of the field in questions then you don’t need to get every post and loop over them, so there shouldn’t be a need for the query, just use get_field_object()
using the field key
$field = get_field_object('field_XXXXXXX');
foreach ($field['choices'] as $value => $label) ..... etc
The topic ‘Display repeater subfield as a dropdown (label + value)’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.