On a recent site I was working on I needed to be able to display a page’s grandparent page name on that page. I wanted to be able to have it display automatically so the end user would not have to add it to the page each time. Here is an example of what I’m talking about.
Suppose you have a sitemap that looks like this:
- Collection One
- Product One
- Product One Detail
- Product One History
- Product Two
- Product Two Detail
- Product Two History
- Product One
- Collection Two
- Collection Three
What I needed was to display the page name ‘Collection One’ on the ‘Product One Detail’ and ‘Product One History’ pages (basically showing that that ‘product’ was part of that ‘collection’). Using the code to display a page’s parent page name I posted before in ‘Using page attributes in concrete5 themes‘ would not work in this situation – it would have displayed the ‘Product One’ page name.
I was able to find this code snippet in the concrete5 forums that worked perfectly:
<?php global $c; $nh = Loader::helper('navigation'); $cobj = $nh->getTrailToCollection($c); ?> <?php $rcobj = array_reverse($cobj); if(is_object($rcobj[1])) { $pID = $rcobj[1]->getCollectionID(); $page = Page::getByID($pID); $title= $page->getCollectionName(); }else{ $title= $c->getCollectionName(); } ?> <h2><?php echo $title; ?></h2>
Source: seanom
http://www.concrete5.org/community/forums/customizing_c5/nested-pages-getcollectionname-of-first-parent