Support

Account

Home Forums Front-end Issues How to display an optgroup in my theme?

Solved

How to display an optgroup in my theme?

  • Hi,

    I checked this post about the displaying an optgroup in the Admin Panel (http://support.advancedcustomfields.com/forums/topic/how-do-i-get-optgroups-in-the-select-field/), but anyone knows how can i display the select element with the optgroups and the options in my theme home page like the example above?

    When i have to show the common select element I use this:

    $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>';
    					}
    

    And how about the optgroup?

    Thanks in advance!

    • Elliot

    • January 30, 2014 at 2:26 pm

    Hi @sidney.gpa

    Your code above loops over the field’s choices and adds them as options.
    How have you saved the choices in the field’s settings?
    Have you formatted the choices so that code could read it into optgroups?

    Thanks
    E

  • 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?

    • Elliot

    • January 31, 2014 at 1:19 pm

    Hi @sidney.gpa

    It is possible to use ACF to render your select field. Then, your front end select field will behave the same as your back end select field.

    You can do so like this:

    
    $field_key_xyz = "field_52e81ce100934";
    $field = get_field_object($field_key_xyz);
    create_field( $field );
    

    Thanks
    E

  • Thank you so much @elliot! It works perfectly! 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.