Jump to content

Solved: Finding am image with its field


pwired
 Share

Recommended Posts

Hi,

In a page I want to find an image that has a specific field with a specific value

In this case the Page = $p and the image that I am looking for has a field = videocounter and the value of this field = 270

I can find the image like this: $image = $p->images->find("videocounter=270");

so far so good

But when I want to show the picture on the front: <img src="<?php echo $image->url ; ?>" />

It doesn't work, the image does not show

With a few simple echos of $image;  and  $image->url; I found a work around that makes the image show up:

<img src="<?php echo $image->url . $image; ?>" />

But I find this an ugly work around

What would be the correct way to make the image show up ?

 

Link to comment
Share on other sites

9 hours ago, pwired said:

<img src="<?php echo $image->url . $image; ?>" />

Wow, I didn’t believe this until I tried it ?

However, since you’re only looking for one image, try using get() instead of find():

$image = $p->images->get("videocounter=270");

Or use first() to get the first matching image:

$image = $p->images->find("videocounter=270")->first();

Otherwise find() will give you a Pageimages object that potentially holds multiple images, so calling url() on it won’t work.

  • Like 1
Link to comment
Share on other sites

Quote

Otherwise find() will give you a PageImages object that potentially holds multiple images, so calling url() on it won’t work.

Thanks @Jan Romero for showing the correct way in this case:

$p->images->get("videocounter=270")->url;


I am aware now that I have to dive deeper into the processwire api with the use of arrays, Pageobjects and selectors ...

Link to comment
Share on other sites

  • pwired changed the title to Solved: Finding am image with its field

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...