Jump to content

Recommended Posts

Hello,

 

First up, the project I'm working on is a WordPress theme using the Advanced Custom Fields Pro plugin. However, I don't think knowledge of this plugin will be totally necessary for somebody to help, or to begin pointing me in the right direction. 

 

I'm trying to get an image gallery to display, that's nested inside what's called a flexible content block (it works similar to a repeater; note the if and while statements). So far I've been unsuccessful. The structure of my fields are as such:

 

  • Field Group
    • Flexible Content (page_layout)
      • Layout (heading_text)
      • Layout (body_text)
      • Layout (image_gallery)
        • Gallery (images)

 

Here's the PHP code:

	<?php

	// check if the flexible content field has rows of data
	if( have_rows('page_layout') ):

	     // loop through the rows of data
	    while ( have_rows('page_layout') ) : the_row();

	        if( get_row_layout() == 'heading_text' ):

	        	the_sub_field('heading_level');
	        	the_sub_field('heading_content');


	        elseif( get_row_layout() == 'body_text' ): 

	        	the_sub_field('body_text_content');


	        elseif( get_row_layout() == 'spacer' ): 

	        	the_sub_field('spacer_height');	


	        elseif( get_row_layout() == 'gallery' ): 

	        	// check if the nested repeater field has rows of data
	        	if( have_rows('images') ):

	        		echo '<ul>';

					// loop through the rows of data
				    while ( have_rows('images') ) : the_row();	       

				    	$image = get_sub_field('images');

				    	echo '<li><img src="' . $image['url'] . '" alt="' . $image['alt'] . '" /></li>';

				    	var_dump ($image);

				    endwhile;
				    
				    echo '</ul>';

				endif;    	 	

			endif;


	    endwhile;

	else :

	    // no layouts found

	endif;

	?>

In wp-admin (front end editing) I've loaded in three images to the gallery. My output is such:

<ul>
<li><img src="" alt="" /></li>bool(false)
<li><img src="" alt="" /></li>bool(false)
<li><img src="" alt="" /></li>bool(false)
</ul>

So it appears the code is 'sort of working', in that it's finding three images represented by the three <li's>. However, note the empty src and alt fields, and that doing a var_dump on $image gives me back bool(false), when there SHOULD be an array. 

 

Does anybody know why the arrays aren't being returned? 

 

If it helps, there's some pretty easy to read documentation for the plugin over here:

 

Flexible content field: http://www.advancedcustomfields.com/resources/flexible-content/

Gallery field: http://www.advancedcustomfields.com/resources/gallery/

 

If anybody could provide some pointers, that would be really great. I'm also keen to know why this isn't working, and what I've done wrong for my own learning. 

 

Thank you. 

Link to comment
https://forums.phpfreaks.com/topic/300976-nested-looparray-problem-in-php/
Share on other sites

The boolean false is from the var_dump(), so I assume the call to get_sub_field() is failing. The docs don't mention any kind of return value so it's kind of anyone's guess as to why... Maybe the get_sub_field_object() would be a better function to use? I've not used ACF in the past, so I'm kind of stabbing in the dark here...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.