tutchi
April 10, 2017, 3:29pm
1
Hi,
I have an image slider with title and hyperlink on the homepage. But I want to have the same content on the blog and article pages. How can I fix this?
<?php foreach($page->slider()->toStructure() as $slider): ?>
<li style="background-image:url(<?php echo $site->url() ?>/content/<?php echo $page->dirname() ?>/<?php echo $slider->image()->html() ?>)">
<div class="center-y">
<?php if(!$slider->text1()->empty()): ?><h3><?php echo $slider->text1()->html() ?></h3><?php endif ?>
<?php if(!$slider->text2()->empty()): ?><a href="<?php echo $slider->url()->html() ?>"><?php echo $slider->text2()->html() ?></a><?php endif ?>
</div>
</li>
<?php endforeach ?>
You can reference a particular page using the page()
helper:
<?php $p = page('home'); ?>
<?php foreach($p->slider()->toStructure() as $slider): ?>
<li style="background-image:url(<?php echo $site->url() ?>/content/<?php echo $p->dirname() ?>/<?php echo $slider->image()->html() ?>)">
<div class="center-y">
<?php if(!$slider->text1()->empty()): ?><h3><?php echo $slider->text1()->html() ?></h3><?php endif ?>
<?php if(!$slider->text2()->empty()): ?><a href="<?php echo $slider->url()->html() ?>"><?php echo $slider->text2()->html() ?></a><?php endif ?>
</div>
</li>
<?php endforeach ?>
Please read the this cookbook recipe for ways to get the image URL from an image name in a structure field.
tutchi
April 10, 2017, 5:26pm
3
Thanks a lot.
What if I would like to have the first item?
<?php $p = page('home'); ?>
<?php foreach($p->slider()->toStructure() as $slider): ?>
<li style="background-image:url(<?php echo $site->url() ?>/content/<?php echo $p->dirname() ?>/<?php echo $slider->image()->html() ?>)">
<div class="center-y">
<?php if(!$slider->text1()->empty()): ?><h3><?php echo $slider->text1()->html() ?></h3><?php endif ?>
<?php if(!$slider->text2()->empty()): ?><a href="<?php echo $slider->url()->html() ?>"><?php echo $slider->text2()->html() ?></a><?php endif ?>
</div>
</li>
<?php endforeach ?>
The toStructure()
´method gives you a collection . You can use (most) methods of the collection class, including first()
to get the first item of the collection.
tutchi
April 10, 2017, 6:01pm
5
When I do this, it gives an error…
<?php foreach($p->slider()->toStructure()->first() as $slider): ?>
Well, a loop does not make sense with a single element…