Auto Nav Display Second Level plus current parent.
Permalink
I currently have an auto nav in a sidebar. My code below shows all pages from the second level and below. If I wanted to display the top most parent of the current page, how would I go about that?
For example, my tree:
Resources
--Downloads
--FAQs
----General
----Selling
--Videos
I want to display Resources at the very top of the auto nav. Right now it's just:
Downloads
FAQs
--General
--Selling
Videos
Any help would be great. Thanks!
For example, my tree:
Resources
--Downloads
--FAQs
----General
----Selling
--Videos
I want to display Resources at the very top of the auto nav. Right now it's just:
Downloads
FAQs
--General
--Selling
Videos
<?php $nh = Loader::helper('navigation'); $path = $nh->getTrailToCollection($c); $c = Page::getCurrentPage(); if ( count($path) > 1) { $revPath = array_reverse($path); $sectionPage = $revPath[1]; } elseif ( count($path) == 1) { $sectionPage = $c; } else { $sectionPage = Page::getByID(HOME_CID); } $cID = $sectionPage->getCollectionID(); $subNavBlock = BlockType::getByHandle('autonav'); $subNavBlock->controller->displayPages = 'second_level'; /* top, second_level, third_level, above, current, below, custom */
Viewing 15 lines of 21 lines. View entire code block.
Any help would be great. Thanks!
This works if I want to display the parent of the current page, but let's say I always want to show the Top Level parent regardless of what child page you're on. How would I do that?
I found one solution, although it may not be the most efficient.
<?php $nh = Loader::helper('navigation'); $path = $nh->getTrailToCollection($c); $c = Page::getCurrentPage(); if ($c->cID > 1) { $arrTrail = array_reverse($nh->getTrailToCollection($c)); $objHomePage = $arrTrail[0]; // Home page object; // If we're on a top-level page then our $arrTrail array will only have the home page object // so we test to see if its Page object exists $objTopPage = is_object($arrTrail[1]) && $arrTrail[1] instanceof Page && !$arrTrail[1]->error ? $arrTrail[1] : $c; // If we have a valid top page Page object we retrieve $title = $objTopPage->getCollectionName(); $url = $nh->getLinkToCollection($objTopPage); // print parent echo '<h5><a class="current" href="' . $url . '"' . '>' . $title . '</a>' . '</h5>';
Viewing 15 lines of 33 lines. View entire code block.
Probably too late but someone might find this useful...
Add this to the top of the autonav:
then you can add this just before the output foreach ni:
A little dirty but works well and efficiently.
Add this to the top of the autonav:
foreach ($navItems as $ni) { if ($ni->level = 1) { $parent = Page::getByID($ni->cObj->getCollectionParentID()); break; } }
then you can add this just before the output foreach ni:
echo '<li class="parent"><a href="' . $parent->getCollectionPath() . '" target="_self" class="parent">' . $parent->getCollectionName() . '</a></li>';
A little dirty but works well and efficiently.
has a lot of useful snippets of code you can use.
Looks like this will work.