Jump to content


Photo

How do I output my top level navigation dynamically?


  • Please log in to reply
5 replies to this topic

#1 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3379

  • LocationAtlanta, GA

Posted 29 December 2010 - 12:02 PM

Dynamic navigation is just a matter of finding the group of pages you want to output, and then loop through them to print the links. For example, if you wanted to print the top navigation:

<ul id='topnav'>
<?php 
foreach($pages->find("parent=/") as $p)
	echo "<li><a href='{$p->url}'>{$p->title}</a></li>";
?>
</ul>


#2 Barry

Barry

    Newbie

  • Members
  • Pip
  • 7 posts
  • 12

  • LocationNetherlands

Posted 21 June 2012 - 06:48 PM

How might I modify this code to display 2 levels of navigation if it exists?

so that it would generate a <ul></ul> within an <li></li> is there were pages under it and keep it at 2 levels? I've seen a post on this on a page level and it works great on the homepage but I can't figure out how to do this on a root level consistently.

#3 renobird

renobird

    Sr. Member

  • Members
  • PipPipPipPip
  • 399 posts
  • 260

  • LocationGainesville, Florida

Posted 21 June 2012 - 07:03 PM

Barry,

You may want to take a look at Soma's MarkupSimpleNavigation module.

:)

--
Tom

#4 Barry

Barry

    Newbie

  • Members
  • Pip
  • 7 posts
  • 12

  • LocationNetherlands

Posted 22 June 2012 - 12:00 AM

I figured it out without a plugin, I'll post it for anyone's future reference:

<ul>
<?php
  
	$homepage = $pages->get("/");
	$mainchildren = $homepage->children;
	$mainchildren->prepend($homepage);

	foreach($mainchildren as $mainchild) {
	 $class = $mainchild === $page->rootParent ? " class='on'" : '';
	 if ($mainchild->mainNav != 0){
	  echo "<li><a$class href='{$mainchild->url}'>{$mainchild->title}</a>";
	  if($mainchild->path != '/' && $mainchild->numChildren > 0) {

	   echo "<ul>";

	   foreach($mainchild->children as $subchild) {
		$class = $page === $subchild ? " class='on'" : '';
		echo "<li><a$class href='{$subchild->url}'>{$subchild->title}</a></li>";
	   }

	   echo "</ul>";
	  }else{
	   echo "</li>";
	  }
	
	 }
	}
   ?>
</ul>

For anyone wondering:

   if ($mainchild->mainNav != 0){

simply checks if a checkbox is checked that I even want it in my main menu.

#5 renobird

renobird

    Sr. Member

  • Members
  • PipPipPipPip
  • 399 posts
  • 260

  • LocationGainesville, Florida

Posted 22 June 2012 - 12:29 PM

Barry,

Nice. I'll give that a test soon and see how it works.
Thanks for posting.

#6 Soma

Soma

    Hero Member

  • Moderators
  • 3,397 posts
  • 1925

  • LocationSH, Switzerland

Posted 22 June 2012 - 02:04 PM

Barry, using my MarkupSimpleNavigation module you could simply do:

$nav = $modules->get("MarkupSimpleNavigation");
echo $nav->render(array(
	"max_levels" => 2,
	"show_root" => true,
	"current_class"  => "on"
));


Just in case. ;)

Edit: Ok the checkbox thing is not possible. But it would be possible using page "hidden" for page you don't want in mainnav. But your code is also nice and flexible if you need it.

@somartist | modules created | support me, flattr my work flattr.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users