Jump to content

Image from


Peter Knight
 Share

Recommended Posts

I'm trying to output a series of photographs onto a index page. Each photo is from a different page so I'm using the $pages API.

Everything is working bar the image and link to the main Graduate detail page.

My PHP call

<?php

$graduates = $pages->find("template=graduate-detail, sort=-date"); 
foreach ($graduates as $graduate)
echo "
    <img src='$graduate->graduate_photo->url' />
    <h3>{$graduate->title}</h3>
    {$graduate->graduate_summary}<br/>
    <a href='{$graduate->url}'>Read more</a>
    <hr/>
"
;?>

What's happening

Instead of outputting a JPG from a field called graduate_photo, i'm left with the following:

<img src="graduate-hazel-scully.jpg->url"> 

As you can see, the image name is correct but the path is incomplete. It's outputting the '->url' too for some reason.

I've red thorugh the PW images documentation so I'm not sure what my issue is. Possiblt it's because the docs are mostly giving examples where an image is called from the $page instead of $pages.

 
Link to comment
Share on other sites

Definitely need to go back to PHP junior school 

Do I need to create a separate variable for the first image and then call that in my echo statement

<?php
$graduates = $pages->find("template=graduate-detail, sort=-date"); 
$myfirstimage = $pages->graduate_photo->first();

foreach ($graduates as $graduate)
echo "
    <img src='{$myfirstimage->url}' />
    <h3>{$graduate->title}</h3>
    {$graduate->graduate_summary}<br/>
    <a href='{$graduate->url}'>Read more</a>
    <hr/>
"
;?>

Above doesn't work but I think Im on the right track

Link to comment
Share on other sites


<?php

$graduates = $pages->find("template=graduate-detail, sort=-date");

foreach ($graduates as $graduate) {

echo "

<img src='{$graduate->graduate_photo->first()->url}' />

<h3>{$graduate->title}</h3>

{$graduate->graduate_summary}<br/>

<a href='{$graduate->url}'>Read more</a>

<hr/>

";

}

?>

  • Like 3
Link to comment
Share on other sites

Adrian - thanks.

I've only spent 5 hours trying to solve this and a couple of hours on the forums. 

Hitting these kinds of roadblocks makes you better in the long run. :)

At some point you'll never forget that when file or image fields are set to allow more then 1 file/image, you'll have to iterate over them because they are a WireArray. Or use a method like adrian showed. Also, this concept is explained at the start of the images documentation you referred to. One of the most common sources of confusion here on the forums :).

  • Like 4
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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