Jump to content

Custom Navigation: display subnavigation only for certain items


joe_ma
 Share

Recommended Posts

Hi

Trying to setup a customized navigation.

Page tree looks like this:

home
– upcoming exhibitions
–– exhibition 1
–– exhibition 2
–– …
– about
– archive
–– arch 1
–– arch 2
–– …
– impressum

Now I'd like to have a navigation, that lists only the subnav of "upcoming exhibitions" but not the one of "archive".

I modified a snippet I found here in the forum. It looks like this so far:

<nav class="mainNav">
<ul>
	<?php
		$homepage = $pages->get('/'); 
		
		//	first item links to the homepage 
		echo "<li><a href='$homepage->url'>Aktuelle Ausstellung / current exhibition</a></li>";

		$children = $homepage->children();

		// render an <li> for each top navigation item
		foreach ( $children as $child ) {
			if ( $child->id == $page->rootParent->id ) {
				// this $child page is currently being viewed (or one of it's children/descendents)
				// so we highlight it as the current page in the navigation
				echo "<li class='active'><a href='$child->url'>$child->title</a></li>";

				// if upcoming page has children, list them
				} elseif($child->id == 1020 && count($child->children)){
					
						// build the subnav and list all items
						echo "<li><a href='$child->url'>$child->title</a><ul class='upcoming'>";
						foreach($child->children as $c){
							echo "<li><a href='$c->url'>$c->title<br>{$c->date_from}–{$c->date_to}</a></li>";
						}	
						echo "</ul><li>";

					// otherwise list only main items
					} else {	
						echo "<li><a href='$child->url'>$child->title</a></li>";
					}
				}
				?>
			</ul>
		</nav>

This puts out the navigation as wanted, but only for main items (see fig. nav-open.png).

As soon as "upcoming exhibitions" or one of its children is active, the navigation collapses (see fig. nav-closed.png).

I cannot find out, where I went the wrong way.

Thanks for help.

nav-closed.png

nav-open.png

Link to comment
Share on other sites

$home = $pages->get('/');
foreach($home->children as $child) {
	$content .= "<a href='{$child->url}'>{$child->title}</a><br>";
	if($child->id !== 1035) {
		if($child->hasChildren) {
			foreach($child->children as $child2) {
				$content .= "- <a href='{$child2->url}'>{$child2->title}</a><br>";
			}
		}
	}
}

page id 1035 would be archive parent

  • Like 3
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...