Can't show loops in the correct order

Hello,
I have the following loop with two conditions to show a different snippet depending on the project’s category. It works but it doesn’t follow of the correct projects order, it will show first all the “infinite” category projects, then all the “slides” ones.

I’m not expert in php, anyone has an idea why this happens? Thank you,

<?php foreach(page('home')->children()->visible() as $project ): ?>

  <?php if($project->category() == 'infinite') { 
      snippet('infinite-loop');
    } else {
      snippet('slides-loop');
    }
  ?>
<?php endforeach ?>

What is the order of the content pages inside the home directory? The output order should be the same as in the Panel with your code.

That’s the issue, the output doesn’t reflect the order in the panel (I also checked in the FTP folder, they’re numbered with the correct order), but it will group the snippets by category.

What does the following code output?

<?php foreach(page('home')->children()->visible() as $project): ?>
  <?php var_dump($project->id()) ?>
<?php endforeach ?>
1 Like

In the foreach, if you don’t output the snippets but, say:

<?php foreach(page('home')->children()->visible() as $project) {
  echo $project->title() . "<br>";
} ?>

do you get the correct order?
(My message was crossed with Lukas’ message, we’re asking the same thing basically.)

1 Like

interestingly the id dump shows the correct order, but the html snippets are still grouped by category:

Hold on, there might have been just a caching issue, will update soon.

Ok, the ordering issue was indeed a caching problem, sorry.

However, now that the order displays correctly, I can’t seem to fetch the snippet content, more precisely:

<?php foreach(page('home')->children()->visible() as $project ): ?>
  <?php if($project->category() == 'infinite') { 
      snippet('infinite-loop');
    } else {
      snippet('slides-loop');
    }
  ?>
<?php endforeach ?>

Snippet ‘infinite-loop’:

<div class="infinite-outer dragscroll">
  <div class="project-section infinite">
    <?php foreach($project->images()->sortBy('sort', 'asc') as $image): ?>
      <img class='' src="<?php echo $image->url() ?>" alt="">
    <?php endforeach ?>
  </div>
</div>

Outcome, it breaks at the first empty loop:

I know I’m a bit out of topic one, but if someone could help I’d really appreciate. Thank you,

Does that mean the first project that doesn’t have images? You should always check that images actually exist.
Do you have debugging turned on ? If so, can you tell us if you get any error messages?

Always test if images exist, before calling any method on an image object:

<div class="infinite-outer dragscroll">
  <div class="project-section infinite">
   <?php if($project->hasImages()): ?>
    <?php foreach($project->images()->sortBy('sort', 'asc') as $image): ?>
      <img class='' src="<?php echo $image->url() ?>" alt="">
    <?php endforeach ?>
  <?php endif ?>
  </div>
</div>
1 Like

It had images, and even after adding the check-for-images the result is the same. It’s almost as it’s not able to fetch/recognize $project, since I had no issues fetching images in previous versions.

Console doesn’t give any error…

Oh, alright, then you probably need to pass the $project variable to your snippet:

<?php foreach(page('home')->children()->visible() as $project ): ?>
  <?php if($project->category() == 'infinite') { 
      snippet('infinite-loop', array('project' => $project));
    } else {
      snippet('slides-loop', array('project' => $project));
    }
  ?>
<?php endforeach ?>

Have you turned on debugging in your site/config/config.php?

c::set('debug', true);
1 Like

That was it. Thanks a lot