getFirstChild as Page title

Permalink 2 users found helpful
Hi, I want to have the first child as an h3 tag within my page template. So when your on the first child or any sub pages of it it shows the page title, i.e.

|Home
|-Page 1
|--Subpage 1
|--Subpage 2
|-Page 2
|--Subpage 3
|--Subpage 4

When you are on Page 1, Subpage 1 or Subpage 2 - Page 1 would be returned between the h3 tags.

When you are on Page 2, Subpage 3 or Subpage 4 - Page 2 would be returned between the h3 tags.

I've tried variations on the below with no avail, any help greatly appreciated.

Regards

<h3><?php $subPage = Page::getByID($c->getFirstChild()); print $page->getCollectionName(); ?></h3>
<h3><?php   print $c->getFirstChild(); ?></h3>

BHWW
 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Try this:
<?php
$title = $c->getCollectionName();
$parentPageId = $c->getCollectionParentID();
if ($parentPageId > 1) {
    $parentPage = Page::getById($parentPageId);
    $title = $parentPage->getCollectionName();
}
?>
<h3><?php echo $title; ?></h3>


The reason this works is because the home page's id is always 1, and the home page's parent id is always 0 (because there isn't one). So if you check the current page's parent id and it's 0, then you're on the home page; if it's 1, then you're on a sub-page. But if it's anything else, you're on a sub-sub-page, in which case you grab the title of the parent page.

Hope that helps!

-Jordan
BHWW replied on at Permalink Reply
BHWW
Hi Jordan, Thanks for that, just the ticket. Also it's really helpful to know why it does it, thanks again, best reply i've ever had ;-)
BHWW replied on at Permalink Reply
BHWW
HI Jordan

Would it be simple to add an <a href=""> to that code to link to the page whose text one is displaying?

<?php
$title = $c->getCollectionName();
$parentPageId = $c->getCollectionParentID();
if ($parentPageId > 1) {
    $parentPage = Page::getById($parentPageId);
    $title = $parentPage->getCollectionName();
}
?>
<h3><a href="<?php echo $titleurl; ?>"><?php echo $title; ?></a></h3>
jordanlev replied on at Permalink Reply
jordanlev
Sure, try this:
<?php
$title = $c->getCollectionName();
$parentPageId = $c->getCollectionParentID();
if ($parentPageId > 1) {
    $parentPage = Page::getById($parentPageId);
    $title = $parentPage->getCollectionName();
    $url = View::URL($parentPage->getCollectionPath());
}  else {
    $url = DIR_REL;
}
?>
<h3><a href="<?php echo $url; ?>"><?php echo $title; ?></a></h3>
BHWW replied on at Permalink Reply
BHWW
Hi Jordan

Thanks for your reply, that works for sub pages but if it's on the main page it return you to the top level (home) page rather than just reloading the current page, It will work for what I need, but just so you know it's not quite right.

Thank you
TheRealSean replied on at Permalink Reply
TheRealSean
I wish I found this earlier now :), An unfruitful search before, meant some dabbling but I did come up with a slightly alternative version using the getTrailToCollection function.

http://www.concrete5.org/index.php?cID=56694...