<?php
// grab your projects
$projects = page('projects')->children()->visible()->limit(6);
// helper variable
$count = 0;
// loop through projects
foreach($projects as $project):
// if the result of the modulo operation is 0, open new row
if($count % 3 == 0): ?>
<div class="row">
<?php endif ?>
<div class="col-3">
<!-- here goes your content -->
</div>
<?php
// if the result of the modulo operation is 2, close row
if($count % 3 == 2): ?>
</div>
<?php endif ?>
<?php $count++; endforeach ?>
I was testing this way of creating new rows and I noticed that the code as posted above does not create a closing </div> for the last row as soon as the last project is a first in a row. So if I have 1, 4, 7, 10 projects, and so on, this code doesn’t validate any more. Can anybody please give me a hint to solve this?
if you use different grid sizes for different viewports you can also add clear fixes with the visible-* classes provided by bootstrap instead of creating multiple rows
I’ve tried that. But there is one problem:
The latest six projects are displayed. But six times instead of one.
It seems that the rows are rotated by 90 degrees.
That’s my code:
<?php
// grab your projects
$projects = page('projekte')->children()->visible()->limit(6);
// helper variable
$count = 0;
// loop through projects
foreach($projects as $project):
// if the result of the modulo operation is 0, open new row
if($count % 3 == 0): ?>
<div class="row">
<?php endif ?>
<!-- here goes your content -->
<div class="col-md-4 col-sm-6">
<?php foreach(page('projekte')->children()->children()->visible()->flip()->limit(6) as $project): ?>
<?php if($image = $project->images()->sortBy('sort', 'asc')->first()): ?>
<a href="<?php echo $project->url() ?>">
<div class="box">
<img class="img-responsive" src="<?php echo $image->url() ?>" alt="<?php echo $project->title()->html() ?>">
<div class="overbox">
<div class="title overtext">
<?php echo $project->title()->html() ?>
</div>
<div class="tagline overtext">
<?php echo $project->tag()->html() ?>
</div>
</div>
</div>
</a>
<?php endif ?>
<?php endforeach ?>
</div>
<!-- the content ends there. -->
<?php
// if the result of the modulo operation is 2, close row
if($count % 3 == 2): ?>
</div>
<?php endif ?>
<?php $count++; endforeach ?>
Hey there!
You’re redefining a foreach loop without taking into account the active project in your first foreach.
Remove the second foreach and you should be good to go!
Note: there’s also a flip in your second loop you may want to apply to your first loop.
I would also recommend one more thing. You’re checking if your project has an image too late in the process to make sure there’ll be 6 projects.
EDIT: hasImages() only works on a page not on a collection.
Try replacing your first foreach loop with: $projects = page('projekte')->children()->visible()->hasImages()->limit(6);
Cleaning up, this will give you:
<?php
// grab your projects
$projects = page('projekte')->children()->visible()->hasImages()->limit(6);
// helper variable
$count = 0;
// loop through projects
foreach($projects as $project):
// if the result of the modulo operation is 0, open new row
if($count % 3 == 0): ?>
<div class="row">
<?php endif ?>
<div class="col-md-4 col-sm-6">
<?php $image = $project->images()->sortBy('sort', 'asc')->first() ?>
<a href="<?php echo $project->url() ?>">
<div class="box">
<img class="img-responsive" src="<?php echo $image->url() ?>" alt="<?php echo $project->title()->html() ?>">
<div class="overbox">
<div class="title overtext">
<?php echo $project->title()->html() ?>
</div>
<div class="tagline overtext">
<?php echo $project->tag()->html() ?>
</div>
</div>
</div>
</a>
</div>
<?php if($count % 3 == 2): // if the result of the modulo operation is 2, close row ?>
</div>
<?php endif ?>
<?php $count++; endforeach ?>