Home › Forums › Front-end Issues › How to display an optgroup in my theme? › Reply To: How to display an optgroup in my theme?
Hey, @elliot.
First, thank you for your attention.
Actually, what I did was to copy all the code from the post I’ve added as a reference (http://support.advancedcustomfields.com/forums/topic/how-do-i-get-optgroups-in-the-select-field/) and when I created a Select Field in the Admin Panel, I added the choices like the post. For example:
-Europe
Germany
France
-South America
Brazil
Argentina
The point is that when I am trying to select the choices when I am adding a post, everything works perfectly. All the “-” choices are written as optgroup and the other choices are options, but when I am trying to display this select in my theme home page, i don’t know what kind of code I have to insert to have the same appearance as the admin panel post select.
I know if I needed just to get the choices of a common select element with no optgroup the code below will work fine, but what code should I need to insert to have the optgroup select element displayed correctly?
$field_key_xyz = "field_52e81ce100934";
$field = get_field_object($field_key_xyz);
if( $field )
{
echo '<select name="' . $field['name'] . '" id="' . $field['name'] . '">';
foreach( $field['choices'] as $k => $v )
{
echo '<option value="' . $k . '">' . $v . '</option>';
}
echo '</select>';
}
Can you help me with that?
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.