Support

Account

Home Forums Backend Issues (wp-admin) Dynamically populate a select field’s choices from the options page EXTENDED

Solved

Dynamically populate a select field’s choices from the options page EXTENDED

  • My question is an extension of Dynamically populate a select field’s choices from the options page, referenced here (example 2).

    On the options page we have a repeater field that collects a title, image, and link. Using the tutorial from the link above, we are able to pull in the title as choices in our checkbox. Once a user selects the options that he/she wants, we’d like to publish this content (image/title/link) that is stored on the options page, but obviously only from the subfields that were selected in the checkbox from the page editor.

    I am having a hard time wrapping my head around this. Any help is greatly appreciated.

  • Hi @jrstaatsiii

    All you will need to do is loop through all the rows from the options page repeater, and look for a match in the $title with that of which was selected on the post.

    Something like this should work:

    
    <?php 
    
    // vars
    $title = get_field('select_field');
    $image = false;
    $link = false;
    
    // find matching row
    if( have_rows('repeater_field', 'option') )
    {
    	while( have_rows('repeater_field', 'option' )
    	{
    		the_row();
    		
    		if( get_sub_field('title') == $title )
    		{
    			$image = get_sub_field('image');
    			$link = get_sub_field('link');
    		}
    	}
    }
    
    // echo
    echo '<img src="' . $image . '" />'
    
    // etc...
    
     ?>
    
  • Hey Elliot, thanks for this push. Since I am using a checkbox which will pass an array instead of a single value, i used this:

    
    // vars
    	
    	$includedCrossSells = get_field('ssm_cross_sells_list', $id);
    	
    	$crossSellTitle = get_sub_field('ssm_cross_sell_title', 'option');
    
    	// find matching row
    	
    	if ( have_rows('ssm_cross_sells', 'option') ) {
    		
    
    		while ( have_rows('ssm_cross_sells', 'option' ) ) {
    				
    			the_row();
    			
    			if ( in_array( (get_sub_field('ssm_cross_sell_title', 'option')), $includedCrossSells ) ) {
    				
    				echo '<a href="' . get_sub_field('ssm_cross_sell_link', 'option') . '"><img src="' . get_sub_field('ssm_cross_sell_image', 'option') . '"</></a>';
    				
    				echo '<p><a href="' . get_sub_field('ssm_cross_sell_link', 'option') . '">' . get_sub_field('ssm_cross_sell_title', 'option') . '</a></p>';		
    				
    			}
    			
    		}
    	
    	}
    

    This works, but what is interesting is that when i pass $crossSellTitle as a variable which takes get_sub_field('ssm_cross_sell_title', 'option'); as a value, instead of using the full get_sub_field('ssm_cross_sell_title', 'option'); it doesn’t work. Can you spot why that is?

    Thank you so much

  • Hi @jrstaatsiii

    Your code:

    
    $crossSellTitle = get_sub_field('ssm_cross_sell_title', 'option');
    

    Is OUTSIDE of the have_rows loop. Therefore, ACF does not know which row of data you want to get the sub field value from! The sub field functions MUST be used WITHIN a has_field of have_rows loop.

    Does that explain it?

  • #facepalm, of course. Thanks

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

The topic ‘Dynamically populate a select field’s choices from the options page EXTENDED’ is closed to new replies.