Support

Account

Home Forums Add-ons Repeater Field How to display radio button options?

Solved

How to display radio button options?

  • Okay, I purchased the ACF Repeater field add-on from you guys, and let me explain what I am trying to do.

    I created a field called ‘resources_column’ on the backend. This is a repeater field with the following sub-fields (and field types):

    resource_title (text field)
    resource_link_type (radio button)
    resource_internal_link (page link)
    resource_external_link (text field)
    resource_description (textarea)

    The resource_link_type field is a radio button with two options:

    internal : Internal Link
    external : External Link

    The field resource_internal_link and resource_external_link are conditional fields that will only display for the end user based upon what option they select. For instance, if I am adding a new repeater row to a page and I select the radio button ‘Internal’ it will accept that condition and then show you the resource_internal_link field.

    Basically what’s going on here is that when adding a new resource link I am giving the editor the ability to either select an internal link (from a list of existing pages in the site) or an external link (they could then enter their own external link to another website).

    Now, the code I have for this in my template looks like this:

    <?php if( have_rows('resources_column') ): ?>
                
                    <?php while( have_rows('resources_column') ): the_row(); 
                        // variables
                        $title = get_sub_field('resource_title');
    					$internal_link = get_sub_field('resource_internal_link');
    					$external_link = get_sub_field('resource_external_link');
                        $description = get_sub_field('resource_description');
    				?>
    				<p class="resource-title"><a href="<?php echo $internal_link; ?>"><?php echo $title; ?></a></p>
                    <p class="resource-description"><?php echo $description; ?></p>
    
                    <?php endwhile; ?>
                
                <?php endif; ?>

    That code works awesome, except I can’t figure out how to enable an if / else if statement that will output either the internal link or external link based on what option they choose.

    Basically somewhere within that code I’d like to have it function sort of like this…

    if option = internal

    echo <p class="resource-title"><a href="<?php echo $internal_link; ?>"><?php echo $title; ?></a></p>

    else if option = external

    echo <p class="resource-title"><a href="<?php echo $external_link; ?>"><?php echo $title; ?></a></p>

    Of course that would have to be contained within that PHP code I posted above (which does work, minus this part of it). Perhaps you could help me out on this one. I know how it should work in my head, I just can’t translate it to PHP since I’m more of a front end guy, and not so much a backend guy.

    Thanks in advance! I love ACF!!!

  • FYI, here is what my setup looks like on the backend…

    Backend

  • Get the link based on the radio field

    
    $resource_link_type = get_sub_field('resource_link_type');
    if ($resource_link_type == 'internal') {
      $link = get_sub_field('resource_internal_link');
    } else {
      $link = get_sub_field('resource_external_link');
    }
    
    

    then show the link

    
    <a href="<?php echo $internal_link; ?>">
    
  • Thanks John, works like a charm! Any way I might be able to add a conditional statement on to that code that makes it so external links use a target=”_blank” in the output so that they open in a new window?

  • 
    $resource_link_type = get_sub_field('resource_link_type');
    if ($resource_link_type == 'internal') {
      $target='self';
      $link = get_sub_field('resource_internal_link');
    } else {
      $taget = '_blank';
      $link = get_sub_field('resource_external_link');
    }
    ?>
    <a href="<?php echo $internal_link; ?>" target="<?php echo $target; ?>">
    
  • Thanks John, I modified it a bit so this is what it looks like now (works perfectly!):

    <?php if( have_rows('resources_column') ): ?>
                
                    <?php while( have_rows('resources_column') ): the_row(); 
                        // variables
                        $title = get_sub_field('resource_title');
    					$internal_link = get_sub_field('resource_internal_link');
    					$external_link = get_sub_field('resource_external_link');
                        $description = get_sub_field('resource_description');
    					$resource_link_type = get_sub_field('resource_link_type');
    					if ($resource_link_type == 'internal') {
    						$link = get_sub_field('resource_internal_link');
    					} else {
    						$target = ' target="_blank" rel="nofollow"';
    						$link = get_sub_field('resource_external_link');
    					}
    				?>
                    
    				<p class="resource-title"><a href="<?php echo $link; ?>"<?php echo $target; ?>><?php echo $title; ?></a></p>
                    <p class="resource-description"><?php echo $description; ?></p>
    
                    <?php endwhile; ?>
                
                <?php endif; ?>
  • Hello. I also have a radio button sub field type within a repeater field.
    Here is my code:

    <?php if( have_rows('outcomes_your_activity_seeks') ): ?>
                
                    <?php while( have_rows('outcomes_your_activity_seeks') ): the_row(); 
                        // variables
                        $radio_1 = get_sub_field('radio_1');
    		    $radio_2 = get_sub_field('radio_2');
    		    $radio_3 = get_sub_field('radio_3');
                        $radio_4 = get_sub_field('radio_4');
    		?>
    		
    		<p class=""><?php echo $radio_1; ?></p>
                    <p class=""><?php echo $radio_2; ?></p>
    
                    <?php endwhile; ?>
                
                <?php endif; ?>

    As I have a few of these custom field setups, is there way to code a loop that displays all sub fields based on the ‘Order’ ‘Label’ ‘Name’ ‘Type’ as opposed to having to put in each unique sub field name? This would prevent a lot of manual coding.

    Also I don’t understand the different between get_sub_field and the_sub_field? One returns a value and the other displays – how are they different?

    My website users are filling out the custom fields form in the WordPress Dashboard. All I need to do is display the content from the form in the front of the website.

    Hoping there is a quick way to display the label + values of the choice/text/select/ sub fields?

  • There isn’t any way that I know of to get all of the sub fields at once. There isn’t a get_sub_fields() function, although it has been requested before http://support.advancedcustomfields.com/forums/topic/get_sub_fields/

    You need to get or display each sub field.

    As far as the difference between get_ and the_, as you said. the_sub_field echoes the value, it does not return anything. Let’s say that a sub field contains a url.

    If you do this

    <a href="<?php 
        the_sub_field('link'); ?>"><?php  the_sub_field('link_text'); ?></a>

    then the url and the text will be output where they belong.

    If you instead tried this

    <a href="<?php 
        get_sub_field('link'); ?>"><?php  get_sub_field('link_text'); ?></a>

    both the link attribute and the text of the link would be blank.

    get_sub_field() is useful when you need to assign the value to a variable, usually because you need to do more with it than simply echo the value.

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

The topic ‘How to display radio button options?’ is closed to new replies.