a-z
January 31, 2020, 3:41pm
1
I am trying the add a different class name to the divs inside the foreach.
This is my code:
<?php
$items = $page->collab()->toStructure();
$count = 0;
foreach ($items as $item): ?>
<?php $class= ($count%2 == 0)?"right":""; ?>
<div class=" '$class' ">
<?php foreach ($item->image()->toFiles() as $image): ?>
<img src="<?= $image->crop(400)->url() ?>">
<?php endforeach ?>
<h3><?= $item->name()->html() ?></h3>
<p><?= $item->projects() ?></p>
</div>
<?php endforeach ?>
Any ideas of what am I doing wrong?
Thanks
AZ
2 issues:
your are not echoing your $class
variable: <div class="<?= $class ?>">
you are missing to increase your counter variable in the loop
But in fact, adding a class is not really necessary to style these items as you can do that with CSS only, using the nth-child()
selector with even
and odd
.
Die :nth-child(an+b) CSS Pseudo-Klasse passt auf ein Element, das im Dokumentbaum an+b-1 Geschwisterknoten vor sich hat, wobei n einen gegebenen positiven Wert oder den Wert 0 hat. Zudem muss das Element einen Elternknoten haben. Einfacher...
a-z
January 31, 2020, 4:09pm
3
oh, that’s right! was over thinking…
nth-child()
works perfectly, many thanks @texnixe