jaspn Posted September 22, 2015 Share Posted September 22, 2015 (edited) 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 Edited September 22, 2015 by jaspn Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted September 22, 2015 Solution Share Posted September 22, 2015 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> Quote Link to comment Share on other sites More sharing options...
jaspn Posted October 16, 2015 Author Share Posted October 16, 2015 @Chocu3r, that solution worked perfectly, thank you. I'm still unsure how it does work, but I'll have to look into it further. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.