OpenBayou Posted January 1, 2017 Share Posted January 1, 2017 Hi, How do you get the title of a parent page? If I use `<?php echo $page->parent->title;?>` it shows the title of the parent page on each post. I'm just looking for a way to show it once. This is what I have so far. <?php foreach($pages->find("parent=recommendations") as $child) { ?> <?php echo $child->title;?> <img src="<?php echo $child->Image->first()->size(200,0)->url;?>"> <?php } ?> Thanks and happy new year. Link to comment Share on other sites More sharing options...
Klenkes Posted January 1, 2017 Share Posted January 1, 2017 Perhaps I misunderstood but what if you put it outside of the foreach loop so it gets output only once? <?php echo $page->parent->title;?> <?php foreach($pages->find("parent=recommendations") as $child) { ?> <?php echo $child->title;?> <img src="<?php echo $child->Image->first()->size(200,0)->url;?>"> <?php } ?> 1 Link to comment Share on other sites More sharing options...
OpenBayou Posted January 1, 2017 Author Share Posted January 1, 2017 Doesn't show anything. `<?php echo $child->parent->title;?>` does show the title but I still have problems. The title of parent page does show up if it's inside the loop (foreach) but it shows the title on each post. Example: if I have two posts, it looks: Parent page title Image Title Parent page title Image Title Looking for a way to show it once. Link to comment Share on other sites More sharing options...
kongondo Posted January 1, 2017 Share Posted January 1, 2017 I am assuming by post you are referring to the $child? What is not clear is whether you want the title of the parent of the current page or the title of the parent of $child. Anyway, here's some code. // will give you the title of the parent of the current page // @note: if on 'home' page, will return nothing since 'home' does not have a parent $page->parent->title; // will give you title of the parent of $child // @note: since you are in loop, this will be repeated for each $child // but you already know the parent. It is 'recommendations'...so, @see alternative code below $child->parent->title; /** alternative code **/ // get the parent page $r = $pages->get('/path/to/recommendations/'); $out = '';// empty variable that we will populate with final output if($r && $r->id && $r->numChildren) {// checks if we found a valid page and it has children $out .= $r->title;// @note: about the .= read up on PHP concatenation (we are adding to the previous value we found here, rather than using multiple echos) foreach ($r->children as $child) { $out .= $child->title; $out .= '<img src="' . $child->Image->first()->size(200,0)->url '">'; } } // output echo $out; Also note, ideally, you want to name your fields all lowercase, i.e. 'image' rather than 'Image'. 3 Link to comment Share on other sites More sharing options...
OpenBayou Posted January 1, 2017 Author Share Posted January 1, 2017 This is too complicated for me but it's a start for me to work on. Link to comment Share on other sites More sharing options...
kongondo Posted January 1, 2017 Share Posted January 1, 2017 It will soon click and it is code you can reuse . Btw, I added a note on concatenation in my code above. 1 Link to comment Share on other sites More sharing options...
OpenBayou Posted January 7, 2017 Author Share Posted January 7, 2017 So this is what I've done so far. It does work but anything better or other suggestions is greatly appreciated. // the parent <?php foreach($pages->find("template=recommendations") as $parent) { ?> <?php echo $parent->title;?> <?php } ?> // children <?php foreach($pages->find("parent=recommendations") as $child) { ?> <?php echo $child->title;?> <img src="<?php echo $child->post_image->first()->size(200,0)->url;?>"> <?php } ?> Link to comment Share on other sites More sharing options...
tpr Posted January 7, 2017 Share Posted January 7, 2017 You could use this // the parent <?php echo $pages->get("template=recommendations")->title;?> // children <?php foreach($pages->find("parent=recommendations") as $child) { ?> <?php echo $child->title;?> <img src="<?php echo $child->post_image->first()->size(200,0)->url;?>"> <?php } ?> or this: <?php // flag to track if parent title was shown $parentTitleShown = false; ?> <?php foreach($pages->find("parent=recommendations") as $child) { ?> <?php if(!$parentTitleShown) { // parent echo $child->parent->title; // set flag $parentTitleShown = true; } // children echo $child->title; ?> <img src="<?php echo $child->post_image->first()->size(200,0)->url;?>"> <?php } ?> 1 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