Jump to content

Repeater field in child page loop keeps showing same field data for all child pages


Recommended Posts

Posted

Something probably off with my loop, the code below is on my "entries.php" template page, it's outputting the title and description of each entry fine but the repeater fields just repeats the first set of images from entry 1 throughout the loop.

 

<?php
// Functions

  $entries = $page->children;
  $entry_array = array();
  $image_array = array();
  
  foreach ($entries as $entry) {
    
    // Define the fields wanted
    $title = $entry->title;

    $description = $entry->entry_description;

    $images = $entry->images;
    foreach ($images as $image) {
        $imageUrl = $image->url; // make a thumb version
        $imageDescription = $image->description;
        // Build array from repeater
        $image_array[] = array(
            'image_url' => $imageUrl,
            'image_description' => $imageDescription,
        );
    }

    $pageUrl = $entry->httpUrl;

    // Build the array from the fields
    $entry_array[] = array(
          'title' => $title, 
          'description' => $description,
          'images' => $image_array,
          'page_url' => $pageUrl,
        );
  }
  
  // Json encode the array
  $entries = json_encode($entry_array, true);
  // Dump the data
  // echo $data;  

?>

 

Posted

I figured this out, it's because I had no 'images' for other entries so the variable array was been set and never re-set/assigned in the loop.

 

Now I have an if (count($images)) wrapper and else I can just spit out the entry array when count is empty.

Posted

So it's not working still, seems to keep building the array rather than just creating the single array for images each time it loops and iteration.

 

How do I get those repeater fields in my for loop for each item in the loop?

Posted

Doh, it's working now...

I had a JS lib that was giving a waring because the json data I was encoding had no array item - I just needed to wrap that with a conditional.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...