gerico
December 13, 2022, 11:13am
1
I’m using the following to fetch info from the next listed and previous listed pages so have a “next project” and “previous project” button on each individual project page:
<div class="project-quicknav [ pad-top-1500 ]">
<div class="quick">
<div class="quick-previous">
<?php if ($page->hasPrevListed()): ?>
<a class="breakout-button" href="<?= $page->prevListed()->url() ?>">Previous Project</a>
<?php endif ?>
</div>
<div class="quick-next">
<?php if ($page->hasNextListed()): ?>
<a class="breakout-button" href="<?= $page->nextListed()->url() ?>">Next Project</a>
<?php endif ?>
</div>
</div>
</div>
Each project has a cover image field - is there anyway I can fetch the cover image from the next and previous project so go alongside the respective next / previous links?
$page->prevListed()
/$page->nextListed()
are the page objects for the prev/next page, so you can get any information from them just like you do it normally.
I’d actually change the logic a bit
<div class="project-quicknav [ pad-top-1500 ]">
<div class="quick">
<div class="quick-previous">
<?php if ($prevPage = $page->prevListed()): ?>
<a class="breakout-button" href="<?= $prevPage->url() ?>">Previous Project</a>
<?php endif ?>
</div>
<div class="quick-next">
<?php if ($nextPage = $page->nextListed()): ?>
<a class="breakout-button" href="<?= $nextPage->url() ?>">Next Project</a>
<?php endif ?>
</div>
</div>
</div>
Then get $prevPage->cover()->toFile()
etc.
gerico
December 13, 2022, 11:38am
4
Thanks for the updated logic.
I’ve managed to get it working via this so far. Although it doesn;t display the image, is it correctly fetching the image name, but lacking the path.
<div class="project-quicknav [ pad-top-1500 ]">
<div class="quick">
<div class="quick-previous">
<?php if ($prevPage = $page->prevListed()): ?>
<a class="breakout-button" href="<?= $prevPage->url() ?>">Previous Project</a>
<img src="<?= $prevPage->content()->get('cover')->crop(560, 380)->url() ?>" alt="">
<?php endif ?>
</div>
<div class="quick-next">
<?php if ($nextPage = $page->nextListed()): ?>
<a class="breakout-button" href="<?= $nextPage->url() ?>">Next Project</a>
<img src="<?= $nextPage->content()->get('cover')->crop(560, 380)->url() ?>" alt="">
<?php endif ?>
</div>
</div>
</div>
If i just do <?= $prevPage->content()->get('cover')->toFile() ?>
and <?= $nextPage->content()->get('cover')->toFile() ?>
I correctly get the images, I am just working out how I can crop them to a specific size, or is that not possible?
gerico
December 13, 2022, 11:40am
5
No matter, worked it out. Thanks for pointing me in the right direction!
Working code:
<div class="project-quicknav [ pad-top-1500 ]">
<div class="quick">
<div class="quick-previous">
<?php if ($prevPage = $page->prevListed()): ?>
<a class="breakout-button" href="<?= $prevPage->url() ?>">Previous Project</a>
<?= $prevPage->content()->get('cover')->toFile()->crop(560, 380) ?>
<?php endif ?>
</div>
<div class="quick-next">
<?php if ($nextPage = $page->nextListed()): ?>
<a class="breakout-button" href="<?= $nextPage->url() ?>">Next Project</a>
<?= $nextPage->content()->get('cover')->toFile()->crop(560, 380) ?>
<?php endif ?>
</div>
</div>
</div>
This can easily fail if the image doesn’t exist anymore. Either use a if statement that makes sure the image exists, or use the nullsafe operator:
<?= $nextPage->content()->get('cover')->toFile()?->crop(560, 380) ?>
gerico
December 13, 2022, 12:24pm
7
pixelijn:
?
Wondered why that wasn’t working, turns out my docker image is using ubuntu 20.04 which doesn’t contain php8!