Jump to content

Nestable Loops


jaspn
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hi, I'm a PHP beginner, so I don't know if this question should be placed in PHP>general or PHP>applications forum. I assume this forum because I'm coding some PHP to integrate a WordPress plugin called Custom Field Suite. I believe the foundation of this code is generic PHP in nature and not plugin specific, so I think somebody should be able to help me regardless of their WP or CFS experience.

 

I've got the basics of this plugin down pat, and have been happily creating custom fields in the WordPress interface and displaying them successfully using the plugin's php code from the API reference page (link below). I've just come across an issue where I need to create a page which allows my client to add any amount of image galleries, with any amount of images inside each gallery. To create this, I've began with a foreach loop called image_galleries containing a gallery_title. Then inside that I've created another foreach loop called gallery_images, which is nested inside image_galleries. The gallery_images foreach loop contains two simple fields for the file url and image alt text.

 

So far my attempt to get this displaying in-browser has failed - only the surrounding HTML code is displayed on the page, and nothing inside the <p> or two <h2> tag is output to HTML. Here's the raw PHP code:

	<p>


		<?php 

			$fields = CFS()->get('image_galleries');
			foreach ($fields as $field) {
			
		?>
		




			<h2><?php echo CFS()->get('gallery_title'); ?></h2>

			<?php 

				$fields = CFS()->get('gallery_images');
				foreach ($fields as $field) {
				
				echo '<a href="' . $field['image_url'] . '" data-lightbox="anstie_images"><img src="' . $field['image_url'] . '" alt="' . $field['image_alt_text'] . '" class="thumbnails" /></a>';

				} //end nested foreach loop




			    
			} //end parent foreach loop

		?>


	</p> 

It's worth noting that the plugin author does have some code reference to integrate loops, but there's nothing on nested loops: http://customfieldsuite.com/docs/loop/. A google search found one post from a few years ago on the subject that contains deprecated code. 

 

My apologies if my explanation is a bit long winded. It's probably a simple solution (which I've not been able to find on my own as yet) but I thought a bit of background knowledge and my method to date may help provide any clues. Your assistance is appreciated.  

 

Many thanks.

jaspn

post-179803-0-10880700-1442927199_thumb.png

Edited by jaspn
Link to comment
Share on other sites

  • Solution

Reading the documentation in the code example at the bottom is this note

 

NOTE: Values of sub-loop fields are returned when using get() on the parent loop!

Meaning when using get on the parent loop (in your case image_galleries) it will return the values of all the nested loops too. So your code will most likely need to be

    <p>


        <?php 
            // gets all the nested loop values too
            $fields = CFS()->get('image_galleries');
            foreach ($fields as $field) {        
        ?>
        




            <h2><?php echo $field['gallery_title']; ?></h2>

            <?php 
                // loop over the nested loop field called 'gallery_images'
                foreach ($field['gallery_images'] as $gallery_image) {
                
                echo '<a href="' . $gallery_image['image_url'] . '" data-lightbox="anstie_images"><img src="' . $gallery_image['image_url'] . '" alt="' . $gallery_image_field['image_alt_text'] . '" class="thumbnails" /></a>';

                } //end nested foreach loop




                
            } //end parent foreach loop

        ?>


    </p>
Link to comment
Share on other sites

  • 4 weeks later...
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.