Jump to content

How do you get title of parent page?


OpenBayou
 Share

Recommended Posts

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

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 } ?>

 

  • Like 1
Link to comment
Share on other sites

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

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

  • Like 3
Link to comment
Share on other sites

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

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 } ?>

 

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