Jump to content

How to add styles on the menu?


Oleg
 Share

Recommended Posts

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

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

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

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