Jump to content

output images from a field on child page


froot
 Share

Recommended Posts

Hello

beginner here and therefore very basic questions. 

I have a template for a child page and on that there's an image field.

How to I output that image on a parent page? 

it must be something like

echo "<img src='" . $page->children->image->url . "'/>";

How does the script know which field to take the input from? Is it whatever I put in "label" when I added the field to the template? 

When do I use "pages" as opposed to "page"?
When do I use "children" as opposed to "child"? when do I use "->" as opposed to brackets?
When do I use "get" as opposed to "render" or just nothing?

Obviously there's something very basic I'm not getting and those things always seem not worth explaining on the internet so please someone explain it to me or push me in the right direction.

Appreciated!

fruid

Link to comment
Share on other sites

37 minutes ago, fruid said:

When do I use "pages" as opposed to "page"?

In a template $page is the current page that your visitor has requested and is being output for them. $pages gives you access to all pages in the system by calling methods like $pages->find() or $pages->get().

37 minutes ago, fruid said:

When do I use "children" as opposed to "child"?

“Child” is only one child. When you call $page->child() it will give you the first child. If your page has multiple children, you may want all of them. That’s when you call $page->children(). If you want only some children, you can pass a selector to the method. See the docs herehttps://processwire.com/api/ref/page/child/ and here https://processwire.com/api/ref/page/children/.

37 minutes ago, fruid said:

When do I use "get" as opposed to "render" or just nothing?

“Render” usually implies that the method generates markup aka HTML code for you. When you use get() you simply get the thing you asked for. It may be a Page object or the contents of some field or something else, depending on the context. ProcessWire has the concept of “Output Formatting”, which means that field values can sometimes also generate/contain markup themselves, without the word “render” showing up anywhere. So it’s not all that clear cut, but that’s the basic idea. ”Rendering“ means taking something that can’t be output to the browser directly and formatting it, usually as HTML.

37 minutes ago, fruid said:

when do I use "->" as opposed to brackets?

Sometimes ProcessWire makes its features available to you as properties AND as methods. This can be convenient but also confusing. You’ll have to consult the docs for each individual case, or just test out what works. If you want to pass arguments/options, you must use the function way. If you’re not familiar with the concepts of properties and methods/functions, perhaps familiarize yourself with PHP basics before reading the ProcessWire docs, or just read the PHP docs as you go along. Or ask in the forums here, PW has an exceptionally friendly community.

Welcome to ProcessWire ?

Edit: To answer your actual question, you cannot do $page->children->image->url, because children is a PageArray. It's a way to collect multiple pages, in this case all the child pages of $page. So when you call children->image, it doesn't know which child page to take the image from. You could do $page->child->image->url. That would give you the image from the first child page. Be aware that there may be no children, or the first child may not have an image. These cases may need to be considered as your website grows.

  • Like 3
Link to comment
Share on other sites

thank you very much!

15 hours ago, Jan Romero said:

You’ll have to consult the docs for each individual case, or just test out what works.

it's good that you mention that cause beginners like me might think there's something evident and totally logic that they are not getting when in fact they should just use trial and error like more experienced PW developers apparently do as well.

I actually managed to the get it to work the way I wanted now. 

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