Jump to content

Can't render image field content from another page


CBE
 Share

Recommended Posts

Hello.

I've setup a page (without a php file) based on a template with some fields.

I am able to render the text fields content from another page (an included php file):

$pages->get('/general-information/')->phone_number

However, I am unable to render the image field content

$pages->get('/general-information/')->front_image->url

I got an error message (method call on null)

The image field is set to allow only one value, and to allow html entity characters

  • Like 1
Link to comment
Share on other sites

@CBE

The fact is that the image field can be either for a single picture or for an array of pictures. Check how you set the image field for front_image.

Read more about this in the documentation.
But in short. For a single picture, the call will be like this:

$image = $page->single_image;
if($image) echo "<img src='$image->url'>";

For an array of pictures, you can choose the first one:

$image = $page->images->first();
if($image) echo "<img src='$image->url'>";

Look to Maximum files allowed and Formatted value

images_field.thumb.png.4b654c6268d3744ccaf4729dfbae5707.png

Link to comment
Share on other sites

The examples provided by @Andy works seamlessly when the page corresponds to the template (using $page). However I'm not able to render the image field content on another page (using $pages). As I said before the image field is set to allow only one value, and to allow html entity characters.

Link to comment
Share on other sites

@CBE

The method of accessing other pages certainly works. Perhaps you have an error somewhere or access to the page is denied. There is little information on this here. Try to break the task into steps, check it step by step.

$my_page = $pages->get('/general-information/');
	if($my_page){
		$my_img = $my_page->front_image;
		if($my_img){
			$my_url = $my_img->url;
		}else{
			echo "Image not acces";
		}
	}else{
		echo "Page not acces";
	}

Or just see what PageArray returns to you

$my_page = $pages->get('/general-information/');
	if($my_page) print_r($my_page); // use var_dump($my_page) for bit more info

 

  • Thanks 1
Link to comment
Share on other sites

It finally worked. I don´t know how. I was setting permissions everywhere. But when I started revoquing unusual permissions again, trying to figure out the cause of the issue, I ended with the code still rendering the url. So I don´t know. Maybe a cache issue on the ProcessWire side. Anyway, thanks @Andy I appreciate your help.

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