Jump to content

Active Navigation CSS Not Working with Multiple Menu Locations


bck2sqr1
 Share

Recommended Posts

I have a site with a main nav bar, on subpages I have a subnav loading up below the main nav bar.  The problem I'm running into is on the subpages the active nav command isn't working on the subnav items (the main nav is staying active so I got that going for me).  I assume the $page->parent->children hierarchy in the PHP is confusing the command, and this is where I'm running into a wall.

Functions file code:

function renderSubNav($children) {

echo "<div id='subnav'>";
foreach($children as $child) {
        
$classy = $child === $page ? " class='current'" : '';
echo "<a$classy href='{$child->url}'>{$child->title}</a>";

}
echo "</div>";
}

PHP file code:

echo <div id='subnavitems' align='right' valign='top'>";

if(count($page->parents) == 0 && $page->numChildren > 0 && $page->template != "254_home") echo renderSubNav($page->children);
if(count($page->parents) == 1 && $page->numChildren > 0 && $page->template != "254_products") echo renderSubNav($page->children);
if(count($page->parents) == 2 && $page->template != "254_products") echo renderSubNav($page->parent->children);
if(count($page->parents) == 3 && $page->template != "254_products") echo renderSubNav($page->parent->parent->children); 

echo "</div>"; 

Any help would be appreciated!

Link to comment
Share on other sites

That's because $page object is not in scope when used in function.

Try changing the line:

$classy = $child === $page ? " class='current'" : '';

to:

$classy = $child === wire('page') ? " class='current'" : '';

There's also a versatile MarkupSimpleNavigation module for outputting navigations.

Link to comment
Share on other sites

All the "special" api variables that you've at your disposal in the template files are actually not that special, they work exactly like other variables only in the scope they are defined in. If you want to access them independent of any scope you need to use the wire('…') function.

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