Autonav template - trouble getting parrent to have $navSelected class when on children
Permalink
Hi There,
I have a custom autonav template for the main menu - I created it by duplicating and modifying another template.
When on the parent page the .current class is applied but when on a child, the current class isn't applied.
Is there a way to show what section you're in by having the autonav template apply the current class to the parent?
Here is my main nav template:
Sorry, just can't seem to work it out.
Cheers
Ben
I have a custom autonav template for the main menu - I created it by duplicating and modifying another template.
When on the parent page the .current class is applied but when on a child, the current class isn't applied.
Is there a way to show what section you're in by having the autonav template apply the current class to the parent?
Here is my main nav template:
<?php defined('C5_EXECUTE') or die("Access Denied."); $aBlocks = $controller->generateNav(); $c = Page::getCurrentPage(); $nh = Loader::helper('navigation'); $isFirst = true; foreach($aBlocks as $ni) { $_c = $ni->getCollectionObject(); if (!$_c->getCollectionAttributeValue('exclude_nav')) { $target = $ni->getTarget(); if ($target != '') { $target = 'target="' . $target . '"'; } if ($ni->isActive($c) || strpos($c->getCollectionPath(), $_c->getCollectionPath()) === 0) { $navSelected='current';
Viewing 15 lines of 41 lines. View entire code block.
Sorry, just can't seem to work it out.
Cheers
Ben
Is there a reason you don't just use the default (built-in) template? It includes a "nav-path-selected" class on all parent links. I don't see anything in your custom template that is functionally different from the default template (although I could be wrong about this).
I think all I did was duplicate the built-in template, rename the class to .current and try to move the class to the <a> element.
Did I remove something else?
Did I remove something else?
OOhhhh.... I see what's going on. It looks like you copied the header_menu template, not the default template (the default template has a whole series of nested <ul> and <li> elements -- an entire site hierarchy for dropdown menus basically, whereas what you have is only a single level of menu items).
[EDIT: See response below which is actually correct]
[EDIT: See response below which is actually correct]
Wait, scratch that -- I misunderstood the code. Actually that $navSelected thing *is* being outputted in the header_menu template, but it's being put on the <li> that surrounds the <a> tags, not the <a> tags themselves.
And in your code, you somehow removed the thing that outputs that $navSelected class. So what you want to do is change this:
...back to this:
And in your code, you somehow removed the thing that outputs that $navSelected class. So what you want to do is change this:
echo '<li class="'.$isFirstClass.'">';
...back to this:
echo '<li class="'.$navSelected.' '.$isFirstClass.'">';
Thanks for that.
Actually, what I've done is move the .current class from the <li> to the <a> which works great:
The issue is that when you're on a child page, the .current class doesn't get applied which means the main nav is not displaying were you are in the site.
Maybe this is this less a function of the autonav template and more a function of the autonav parameters in header.php?
header.php looks like this:
Actually, what I've done is move the .current class from the <li> to the <a> which works great:
echo('<a class="'.$navSelected.'" href="' . $pageLink . '" ' . $target . '>' . $ni->getName() . '</a>');
The issue is that when you're on a child page, the .current class doesn't get applied which means the main nav is not displaying were you are in the site.
Maybe this is this less a function of the autonav template and more a function of the autonav parameters in header.php?
header.php looks like this:
<?php $autonav = BlockType::getByHandle('autonav'); $autonav->controller->orderBy = 'display_asc'; $autonav->controller->displayPages = 'top'; $autonav->render('templates/main_nav'); ?>
You have an "if" statement just before your <a> link that is preventing your modified code from being displayed on any page other than the "current" page. So change this:
...to this:
...and you should be good to go!
...to this:
echo('<a class="'.$navSelected.'" href="' . $pageLink . '" ' . $target . '>' . $ni->getName() . '</a>');
...and you should be good to go!
Thanks matey - worked great.
Took me a couple of looks to get rid of the entire ie/else - in the end my fiancee worked it out!
Took me a couple of looks to get rid of the entire ie/else - in the end my fiancee worked it out!