bck2sqr1 Posted July 16, 2015 Share Posted July 16, 2015 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 More sharing options...
Fokke Posted July 16, 2015 Share Posted July 16, 2015 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 More sharing options...
bck2sqr1 Posted July 16, 2015 Author Share Posted July 16, 2015 That was it! Seems like it's always something easy like that after I've spent hours trying to troubleshoot Thanks a bunch! 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted July 16, 2015 Share Posted July 16, 2015 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now