<ul id='topnav'>
<?php
foreach($pages->find("parent=/") as $p)
echo "<li><a href='{$p->url}'>{$p->title}</a></li>";
?>
</ul>
How do I output my top level navigation dynamically?
Started by ryan, Dec 29 2010 12:02 PM
5 replies to this topic
#1
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:
#2
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.
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
Posted 21 June 2012 - 07:03 PM
#4
Posted 22 June 2012 - 12:00 AM
I figured it out without a plugin, I'll post it for anyone's future reference:
For anyone wondering:
simply checks if a checkbox is checked that I even want it in my main menu.
<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
Posted 22 June 2012 - 12:29 PM
Barry,
Nice. I'll give that a test soon and see how it works.
Thanks for posting.
Nice. I'll give that a test soon and see how it works.
Thanks for posting.
#6
Posted 22 June 2012 - 02:04 PM
Barry, using my MarkupSimpleNavigation module you could simply do:
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.
$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












