Jump to content

Can't access images array from external page


polly
 Share

Recommended Posts

Hello everybody!

I've been using processwire for more than 1 year and I love it.

Recently I've had this problem:

I have some pages using the basic template with headline, body and images.

I need to access their data from an external page that I put outside the templates folder.

In this external page I initialize the engine this way:

 include("../../index.php"); 
 
 
And I get the page I need like that:
 
$internalpage=$wire->pages->find("id=".$pageid.",include=all")->get(0);
 
I can read the headline and the body of the page normally, but the images array is NULL
 
I try to access the images array as usual:
 
foreach($internalpage->images as $image){
...
}
 
The images field is called "images", like the default one.
 
I've also tried to get the field list of the page, I get the field images but still it is null.
I checked the admin and the images are correctly uploaded on the page I try to access.
 
What am I doing wrong?
 
Thank You!
 
Link to comment
Share on other sites

If you know the pageid, you only need to get the page:

// get the page for that id
$internalpage = $wire->pages->get($pageid);

// now check if the page could be found
if ($internalpage->id != $pageid) return false;

// you can also check for the id not 0
if ($internalpage->id == 0) return false;

So, assuming your $pageid is right, you will pass the lines above and you should be able to access your images field as usual:

$images = $internalpage->images;

// lets check if there are images available or if the field in this page is empty, e.g. not uploaded any image to it
if (count($images)) {
    foreach($images as $image) {
    // do stuff with the images
        ...
    }
}

BTW, welcome to the forums. :)

  • Like 2
Link to comment
Share on other sites

Thank you Horst!

Your way of getting a page by id is much simpler than mine and adding to my code your check for  the lenght of the array, made me realize the actual mistake! 

I was loading my page looking for the wrong page id !!

Such a silly mistake.

Thanks a lot!

  • 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

×
×
  • Create New...