OpenBayou Posted January 19, 2017 Share Posted January 19, 2017 I have three pages under a child page under a child page. Deals - Store - - page 1 - - page 2 How do I show the title of page 1 and page 2? Thanks. This is how I have the template: <?php foreach($pages->find("parent=deals") as $child) { ?> <?php echo $child->title;?> <?php }?> Link to comment Share on other sites More sharing options...
Harmen Posted January 19, 2017 Share Posted January 19, 2017 (edited) If you have the 'Deals' page, you can with $page->children get the 'store' page. Then get the children of the store page and output the title: $store = $pages->find("parent=deals"); echo "$store->title"; $storechild = $store->children; foreach ($storechild as $child){ $content .= $child->title; // OR echo "$child->title"; } Edited January 19, 2017 by Harmen Updated 3 Link to comment Share on other sites More sharing options...
MindFull Posted January 19, 2017 Share Posted January 19, 2017 8 hours ago, Harmen said: $store = $pages->find("parent=deals"); Not to nitpick, but remember that $pages->find will give you a pageArray so it is possible that the $store->title might not work how you'd expect. If another child of Deals exists, then you might run into some errors. It would be better to use either $pages->findOne or $pages->get. If you anticipate that Deals might have more child pages, I would suggest expanding the selector as well. 2 Link to comment Share on other sites More sharing options...
MindFull Posted January 19, 2017 Share Posted January 19, 2017 8 hours ago, OpenBayou said: This is how I have the template: Just curious, is there a particular reason to why you use opening and closing php tags for each line of code? 1 Link to comment Share on other sites More sharing options...
Harmen Posted January 19, 2017 Share Posted January 19, 2017 2 minutes ago, MindFull said: $pages->get Completely true. When I wrote that snippet I tried to continue on the code @OpenBayou provided in his post. Normally I use 'get' Link to comment Share on other sites More sharing options...
OpenBayou Posted January 19, 2017 Author Share Posted January 19, 2017 Is there a difference between 'get' and 'find'? Also I changed 'find' to 'get' and I got an internal server error. Link to comment Share on other sites More sharing options...
fbg13 Posted January 19, 2017 Share Posted January 19, 2017 https://processwire.com/api/ref/pages/ 1 Link to comment Share on other sites More sharing options...
OpenBayou Posted January 19, 2017 Author Share Posted January 19, 2017 12 hours ago, Harmen said: If you have the 'Deals' page, you can with $page->children get the 'store' page. Then get the children of the store page and output the title: $store = $pages->find("parent=deals"); echo "$store->title"; $storechild = $store->children; foreach ($storechild as $child){ $content .= $child->title; // OR echo "$child->title"; } Didn't work. Link to comment Share on other sites More sharing options...
fbg13 Posted January 19, 2017 Share Posted January 19, 2017 $store = $pages->find("parent=/deals/"); Try with parent between forward slashes. Link to comment Share on other sites More sharing options...
OpenBayou Posted January 19, 2017 Author Share Posted January 19, 2017 <?php $store = $pages->get("parent=/deals/"); $storechild = $store->children; foreach ($storechild as $child){?> <?php echo $child->title;?> <?php } ?> The above code does show sub-pages under Page A but sub-pages under Page B and C, it only shows 'Sub-page A Sub-page B'. This is how it's structured: Deals - Page A - - Sub-page A - - Sub-page B - Page B - - Sub-page A - Page C - - Sub-page A Link to comment Share on other sites More sharing options...
kongondo Posted January 19, 2017 Share Posted January 19, 2017 (edited) $pages->get() will always get you ONE page only. In your case, it gets you the first child of $store, i.e. Page A. This is because Page A, via sorting, comes first (i.e. before Page B and Page C. $pages->get() returns a Page. If you did a $pages->find(), this will always return several pages (if it found them, of course). The important thing, as noted earlier, is that $pages->find() returns a PageArray. That means, several Page Objects. So, you cannot directly echo like so: $results = $pages->find('template=basic-page'); // cannot do this; it doesn't make sense since you have multiple items inside $results //...so, you would need to loop through it using a foreach echo $results->title; Think of it this way: // FRUIT: this is very specific. You are requesting a banana; not an orange, not an apple, but a banana. //...We will get you a banana $fruit = $pages->get('banana');// @note: pseudo code! // hence, you can do echo $fruit->title;// outputs 'banana' // BUT....if you want FRUITS...there are several fruits... $fruits = $pages->find('fruits');// @note: pseudo code! // if you did this, the question would be, what fruit was that again? // direct 'echo' doesn't make sense; // ...you need to tell us what fruit you want; apple? orange? banana? There's several fruit in here! echo $fruits->title; // aaah, so, we loop through the basket, one fruit at a time... foreach($fruits as $fruit) echo $fruit->title;// this will output each fruit in here in turn...apple; orange, banana, etc... Hope this makes sense. Edited January 19, 2017 by kongondo 3 Link to comment Share on other sites More sharing options...
fbg13 Posted January 19, 2017 Share Posted January 19, 2017 You're echoing the titles of Page A, B and C not their subpages. $store = $pages->get("parent=/deals/"); foreach ($store->children as $child){ foreach($child->children as $grandchild) { echo $grandchild->title; } } Link to comment Share on other sites More sharing options...
OpenBayou Posted January 20, 2017 Author Share Posted January 20, 2017 Still blank. Link to comment Share on other sites More sharing options...
OpenBayou Posted January 20, 2017 Author Share Posted January 20, 2017 Got it working! Thanks for the help <?php foreach($pages->get("/deals/")->children as $post){ foreach($post->children as $child) {?> <?php echo $child->title;?> <?php } }?> 1 Link to comment Share on other sites More sharing options...
MindFull Posted January 21, 2017 Share Posted January 21, 2017 Congratulations! It gets easier on each iteration (no pun intended) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now