I am using the Page Build to add different styled sections to a blog post but it’s loading the same image into the page for all the images:
This is two examples of code:
<?php if($image = $page->image()): ?>
<div class="medium-block">
<div class="img-box">
<img src="<?= $image->url() ?>" alt="<?= $page->leftimagetextalt()->kt() ?>">
</div>
<div class="text">
<p><?= $data->text() ?></p>
</div>
</div>
<?php endif ?>
and
<?php if($image = $page->image()): ?>
<figure class="full-width">
<img src="<?= $image->url() ?>" alt="<?= $page->fullwidthimagealt()->kt() ?>">
</figure>
<?php endif ?>
Pretty sure it maybe this part: <?php if($image = $page->image()): ?>
but I’m not a 100%
texnixe
2
$page->image()
always fetches the first image in the filesystem. What file do you want to load in each snippet?
The one that has been picked within the builder module
texnixe
4
Then you have to get it from the field and call the toFile()
method on the field value.
if($image = $data->the_field_with_the_picked_image()->toFile):
echo $image->url();
endif
To note: You need $data
here, not $page
.
I’m trying this but it’s not working :
<?php if($image = $data->fullwidthimage()->toFile): ?>
<figure class="full-width">
<img src="<?php echo $image->url(); ?>" alt="<?= $data->fullwidthimagealt()->kt() ?>">
</figure>
<?php endif ?>
texnixe
6
Hm, could you post the code where you call the snippets?
That would be:
<div>
<?php foreach($page->builder()->toBuilderBlocks() as $block): ?>
<?php snippet('blocks/' . $block->_key(), array('page' => $block)) ?>
<?php endforeach ?>
</div>
I tried this with both page
and data
texnixe
8
Well, the problem is that you pass your $block
variable to page
, but then within the snippet, you use $data
.
Change this line to:
<?php snippet('blocks/' . $block->_key(), array('data' => $block)) ?>
Got this all working now, thank you so much @texnixe