Get image from different page than current page

I have a set of blog posts, most of which have a project associated with them. If the blog post has an image, that is shown. If the blog post has no image, but it has a corresponding project, then the first image of that project is shown. At least, that’s what I’m trying to create.

This is the relevant piece of code:

    <? elseif($article->project() != ''):
        $testimage = page($article->project())->images()->sortBy('sort', 'asc')->first(); ?>
        <p>
            <?= $testimage->thumb(array('width' => 600)); ?>
        </p>
    <? endif; ?>

However, the line $testimage = page($article->project())->images()->sortBy('sort', 'asc')->first(); ?> does not work. This is the output: Error thrown with message "Call to a member function images() on boolean".

How do I access an image from another page?

What does &article->project() return? Und where are these project pages in your file tree?

That returns the string of the project associated with the article.

My code:

    <? elseif($article->project() != ''):
        $testimage = $article->project(); ?>
        <p>
            <?= $testimage ?>
        </p>
    <? endif; ?>

The output is <p>projectname</p>.

The file tree is roughly like this:

|-home
    |-article 1
        |-article.txt
    |-article 2
        |-article.txt
        |-image.jpg
|-some page
|-projects
    |-project 1
        |-project.txt
        |-image.jpg
    |-project 2
        |-project.txt
        |-image.jpg

Well, yes, but what does the string contain? The UID of the project or the path to the page? Because the page() helper requires a path to a page. If you store the UID (i.e. only project-a for example), page('project-a') will not get you anywhere, you need to get the page like this:

page('projects/project-a')

And don’t forget to use an if statement to test for $testimage before calling the thumb method.

1 Like

Ah, right. Thank you so much for the quick reply!