Jump to content

Recommended Posts

Posted (edited)

Doing a portfolio website for my self.

Each project has a "cover_img" inside a block first thing on page.

I want to make an array on front page that find template basic page and give me the photo and the title for each page.

the tilte works but not the photo.. I simply do not know how to say that the "cover_img" is inside the $block inside $page.
Can some one help me?
Here is the code:

 

<section class="rpb-archive" <?= $block->styles() ?> <?= alfred($block) ?>>
<?php
 
$link = $pages->find("template=basic-page, limit=9, sort=parent.sort, sort=sort");
 
foreach ($link as $content) {
echo "<div class='content_link'>";
echo "<div class='content_link_wrap'>";
 
$image = $content->$block->cover_img; // Access the block property dynamically
if ($image) {
$cover_image = $image->size(0, 2000);
echo "<a href='{$content->url}'><img src='{$cover_image->url}' alt='{$cover_image->description}'></a>";
}
 
echo "<a class='title' href='{$content->url}'><h2 class='content_text'>{$content->title}</h2></a>";
 
echo "</div>";
echo "</div>";
}
 
?>
</section>
Edited by Atlasfreeman
Posted

Because you're referencing the image dynamically, it's hard to say what it should be here. What you can do though is, if you have the Tracy Debugger module installed, do a few db() calls on $content to try to identify what is actually contained in the value you're retrieving within $link. Try $content, $block, $content->$block, etc. I suspect that you are just not referencing the correct object name, but since you're calling things dynamically, you'll need to do some debugging of your own to figure out what's going on.

  • Like 1
Posted

Could it be that you acutally use a multi-image field aka an image field that allows multiple images?

If so that image part needs to be in a foreach() or you need to use $image->first() and go from there.

// probably easier to use first() here as you only want to show one picture
$image = $content->$block->cover_img; // Access the block property dynamically
if ($image) {
	$cover_image = $image->first()->size(0, 2000);
	echo "<a href='{$content->url}'><img src='{$cover_image->first()->url}' alt='{$cover_image->first()->description}'></a>";
} 

 

  • Like 1
  • 1 month later...
Posted

In the this was the code i used:

 

<?php
echo "<div class='archive_wrapper'>";
$link = $pages->find("template=basic-page|basic-page-book, sort=parent.sort, sort=sort");
 
foreach ($link as $content){
echo "<div class='content_link'>";
echo "<div class='content_link_wrap'>";
 
$block_img = $content->cover_img;
if ($block_img) {
echo "<a href='{$content->url}'><img class='maxheight' src='{$block_img->url}' alt='{$block_img->description}'></a>";
}
 
echo "<a class='title' href='{$content->url}'><h2 class='content_text'>{$content->title}</h2></a>";
 
echo "</div>";
echo "</div>";
 
}
echo "</div>";
?>
  • Atlasfreeman changed the title to [SOLVED] Getting image from other page that are made in a block 8-S

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...