Jump to content

Trouble getting an image through the API


buddy
 Share

Recommended Posts

Hi all,

I'm having trouble getting the URL for an image using the API.

I have an image field in my template and I've uploaded an image, but I can't access it the usual way that has been suggested on this forum, which would be:

$page->images

In my code, the page object doesn't have any child called images:

$page = $wire->pages->get("/my-page/");
$fields = $page->getInputFields();
print_r($fields->get("some_other_field")->value); //works great
print_r($page->images); //nothing here
print_r($fields->get("the_image_field")->value); //contains a protected data object which has a child that contains the URL I want

So, the image URL is definitely buried within the data, but $page->images simply doesn't exist for me to access it the normal way.

Any ideas about what I might be doing wrong?

Thanks!

Link to comment
Share on other sites

Hi Buddy and welcome!

$page = $wire->pages->get("my-page");

Not sure about the context, but I assume you work at template files (/site/templates/*.php), If that is the case, then above line doesn't make a lot of sense. $page variable is already populated with the current page, so you should use different variable there. Also $wire->pages should be just $pages. So this might be more accurate:

$p = $pages->get("/my-page/"); // I use $p instead of $page so that I won't override the current page

$page->getInputFields() is not needed here at all I think. All the fields are directly accessible from the Page object, like this:

echo $page->title;

But images are different from norman text fields. This doesn't return url in any case:

$page->images

Above returns either Pageimage object or Pageimages object (=array), depending on how many images it is allowed to have. If 1 then it is single image, otherwise it is array. So most common way to get first image is this:

$page->images->first()->url;

Recommended reading: http://processwire.com/api/templates/ and http://processwire.c...in-processwire/

Link to comment
Share on other sites

Hi Apeisa, thanks for the quick reply. I have it working now.

I'm not working within the template file, and the $page->images object does not exist;

print_r($page->images);

is empty. There is no Image object or ImageArray there.

However, it seems like I was making a mistake earlier, because after re-testing my code,

$fields->get("the_image_field")->value

started working. I'm not sure why it didn't before. Maybe I had a typo or some other mistake.

Also, thanks for the tip about not needing the getInputFields call. I've re-written the code like this, and it works:

$photo_url = $page->the_image_field;
Link to comment
Share on other sites

$page->images would only work of your images field was called "images" otherwise as you say it would be "the_image_field" if that's what you called your image field.

$page->image_field_name->url gets you the URL and ->description on the end instead of url gets you the image description if you entered one.

  • Like 1
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...