Jump to content

Return content from certain template but only from child pages


Franko
 Share

Recommended Posts

Here is my current code

<?php foreach($pages->find('template=basic-page') as $c): ?>
<li><a href="<?=$c->url;?>"><img src="<?=$c->listImage->url;?>" alt="<?=$c->listImage->description;?>" width="100" height="100"> <?=$c->listImage->description;?> <span>View More</span></a></li>
<? endforeach; ?>
 
What I don't know how to do is return content from the template basic-page but only from child pages.
 
I'm thinking maybe I can just return the fields from the children pages without calling the template but I do not know how. 
 
Suggestions would be appreciated. 
Link to comment
Share on other sites

Sounds a little confusing - all pages, except Home, are child pages of something, so it all has to be relative to the parent page you're interested in.

If you know the parent you can do:

foreach($pages->get("parent=xxxxxxx")->children() as $c){

Maybe if you provide the page tree structure for us we can help figure out an efficient way.

Link to comment
Share on other sites

Home

 Page A 

   Child  Page

   Child  Page

 Page B

   Child Page

   Child Page

All Child pages have the same template. I want to return listimage field from child pages but only to their parents. 

Link to comment
Share on other sites

Well I think this should work:

foreach($pages->find("template=basic-page, parent!=/") as $c)

That will get all pages with basic-page template but without home (/) as their direct parent. Of course this won't work if you end up with further levels of child nesting. But right now, Page A and Page B should be excluded by this.

  • Like 1
Link to comment
Share on other sites

Oh I see what you did and that works by returning list image from home page but I would to return listimage from child pages as in Page A as Parent. So in other words I want Page A to display listimage from its child pages and I want page B to display listimage only from it's child pages. 

Link to comment
Share on other sites

Oh I see, so your selector is relative to PageA and PageB as the current $page ?

In that case this should work when run from PageA or PageB:

foreach($page->children() as $c){

or

foreach($page->children("template=basic-page") as $c){

Do the "Child Pages" ever get loaded by their URL?

If so and because they have the same basic-page template, you need to do something else to prevent them for looking for child pages.

You could simply do:

if($page->numChildren()>0){
    foreach($page->children() as $c){
}

Or perhaps it would actually make more sense to have a separate template for the PageA and PageB parents?

Lots of options really :)

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