Support

Account

Home Forums General Issues Use get_sub_field outside have_rows

Solved

Use get_sub_field outside have_rows

  • Hey guys! First of all thanks in advance for any help provided!

    I’m wondering how would you guys go to get a field from outside the ACF loops?

    I was getting a soft error with the explained way of handling flexible content and gone to use a normal foreach loop to get rid of it… It’s all great except trying to access the video (oembed) sub field which doesn’t give me the object but displays the video (iframe) directly.

    I was using get_sub_field(‘video’, false, false) before inside the have_rows(). Now I don’t know how to get it… I’ve tried everything I know already…

    Any idea?

    Thanks a lot!!

  • That would depend on how your getting the values for the other sub fields of the flex field. Can you provide the loop you are using?

  • Hey, here it is:

    $content_blocks = get_field('content_blocks', $project->ID);
    
    if( $content_blocks ):
    	foreach ($content_blocks as $key => $content_block): 
    
    		$layout = $content_block['layout'];
    		$block_type = $classes[ $content_block['acf_fc_layout'] ];
    
    		$start = $content_block['start_at'];
    		$stop = $content_block['stop_at'];
    
    		if( $block_type == 'block__image' ): 
    
    			$image = $content_block['image'];
    			$image_url = $image['sizes']['post-thumbnail'];
    			...
    
    		elseif( $block_type == 'block__video' ):
    			// Here is the problem... can't get the video the same way as with the image
    			$video = $content_block['video'];
    			// $video_url = get_sub_field('video', false, false);
    			...
    
    		endif; 
    	endforeach;
    endif;
  • so, when you were using

    
    $video_url = get_sub_field('video', false, false);
    

    the second false in this function call tells ACF not to format the value, so it’s returning just the URL.

    Now you are getting the repeater (flex field) using

    
    $content_blocks = get_field('content_blocks', $project->ID);
    

    and ACF is formatting the value of the oEmbed field so instead of returning the URL it’s returning the full iFrame, or at least is should be because that’s why I’m getting when I test this.

    So, here are your choices as I see them. Do what you doing and get unformulated values for every sub field

    
    $content_blocks = get_field('content_blocks', $project->ID, false);
    

    This will mean that you’ll need to manually format all of the fields, for example the image field will only return the image ID and you’ll need to use WP function to get other image information.

    The other choice is to go back to using the ACF have_rows loop and figure out how to correct whatever error that was giving you.

  • Thanks a lot for the extensive explanation! I actually tried your first choice before but when I looked at it I got scared! Haha! Nothing had names, nothing… I thought there must be a way I’m not seeing… seems not… :/

    But yeah, I think the first choice will do the trick with a bit more work…

    The second, just for you to know, not sure if you’re from the ACF team and you’re interested, but the error was related to this thread. There seems to be no reason for it from what I see from people. Well, a reason may be not using the “while have_rows” immediately after the “if have_rows”, I don’t know, but that’s not an option for me because I need something in between.

    Well, again, many thanks for the quick answers!
    Cheers!

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

The topic ‘Use get_sub_field outside have_rows’ is closed to new replies.