Jump to content

menu with 2 levels, a dropdown, active flags and …


ngrmm
 Share

Recommended Posts

i want to output a menu with just one dropdown-option and an class="active" if the user is on the active page

this is my tree

– root

–– page A

–––– child page A

–––– child page A

–––– …

–– page B

––– child page B

––– child page B

––– child page B

–– page C

––– child page C

––– child page C

and the output should be like this
 

–– page A (just dropdown, no link! just text)

–––– child page A (link)

–––– child page A (link)

–––– …

–– page B (link)

(children should not be listed)

–– page C (link)

(children should not be listed)

and if the current page matches A, B and C matches on any level a class should be added to page A or B or C

this my code so far

but i dont how to make page B and C an link and page A just text

and how not to output the children of page B and C

 <?php
        $root = $pages->get("/");
        $children = $root->children();
        foreach($children as $child) {
			$class = $child === $page->rootParent ? " class='on'" : "";
			echo "<li $class ><a  >{$child->title}</a>";
			echo "<ul>";
				foreach($child->children as $grandchild) {
					echo "<li ><a href='{$grandchild->url}'>{$grandchild->title}</a></li>";
					}
			echo "</ul></li>";
					
			}
				
        ?>

Link to comment
Share on other sites

if(in_array($child->id, $dropdown_id_array)){
  // echo text + children
}else{
  // echo link and no children
}

As long as you want to arbitrarily exclude children from the menu you need to specify the pages with dropdowns in code. You could also use any field value of those pages if there's one for differentiating between dropdown/no-dropdown pages.

  • Like 1
Link to comment
Share on other sites

this works, but it might be not the best or shortest solution

	<ul>
        <?php
        $root = $pages->get("/");
        $children = $root->children();
        foreach($children as $child) {
			$class = $child === $page->rootParent ? " class='on'" : "";
			if( $child->id == $pages->get("/page_A")->id ) {
				echo "<li $class ><a  >{$child->title}</a>";
			}
			else {
				echo "<li $class ><a href='{$child->url}'>{$child->title}</a>";
			}
			echo "<ul>";
				if( $child->id == $pages->get("/page_A")->id ) {
				foreach($child->children as $grandchild) {
					echo "<li ><a href='{$grandchild->url}'>{$grandchild->title}</a></li>";
					}
				}	
			echo "</ul></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

×
×
  • Create New...