Oleg Posted May 19, 2015 Share Posted May 19, 2015 Can anyone help with the styles in the menu? How to add a class "active" only for active links? li.active { background-color: #D74145;} <ul id="topnav"> <?php $root = $pages->get("/"); $children = $root->children(); // insert the following line $children->prepend($root); foreach($children as $child) { echo "<li><a href='{$child->url}'>{$child->title}</a></li>"; } ?> </ul> Link to comment Share on other sites More sharing options...
tpr Posted May 19, 2015 Share Posted May 19, 2015 You will need something like this (untested!): <ul id="topnav"> <?php $root = $pages->get("/"); $children = $root->children(); // insert the following line $children->prepend($root); foreach($children as $child) { $activeClass = ($child->id == $page->id) ? ' class="active"' : ''; echo "<li><a href='{$child->url}' $activeClass>{$child->title}</a></li>"; } ?> </ul> Link to comment Share on other sites More sharing options...
SiNNuT Posted May 19, 2015 Share Posted May 19, 2015 I would suggest you study the template files that come with one of the PW default profiles. There is a lot there to learn and also stuff that easy to copy paste and adjust a bit to your needs. Taken straight from _main.php of the of site-default: $homepage = $pages->get('/'); //this comes from _init.php <ul class='topnav'><?php // top navigation consists of homepage and its visible children foreach($homepage->and($homepage->children) as $item) { if($item->id == $page->rootParent->id) { echo "<li class='current'>"; } else { echo "<li>"; } echo "<a href='$item->url'>$item->title</a></li>"; } // output an "Edit" link if this page happens to be editable by the current user if($page->editable()) echo "<li class='edit'><a href='$page->editUrl'>Edit</a></li>"; ?></ul> Link to comment Share on other sites More sharing options...
Oleg Posted May 19, 2015 Author Share Posted May 19, 2015 Thank you for your help 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