Jump to content

Page level and sub-navigation


Soma
 Share

Recommended Posts

Hi, I'm new to PW and php, and i need your help guys. My problem is im trying to include the "Home" menu to this code below that

slkwrm posted..can anyone help me?..tnx

function treeMenu($page, $depth = 1, $id = null) {

$depth -= 1;

if(is_null($page)) $page = wire('page');

if(!is_null($id)) $id = " id='$id'";

$out = "\n<ul$id>";

$parents = $page->parents;

// This is where we get pages we want. You could just say template!=news-item

foreach($page->children() as $child) {

$class = "level-" . count($child->parents);

$s = '';

if($child->numChildren && $depth > 0 ) {

$s = str_replace("\n", "\n ", treeMenu($child, $depth));

}

$class .= " page-{$child->id}";

$class = " class='$class'";

$out .= "\n <li$class>\n <a$class href='{$child->url}'>{$child->title}</a>$s\n </li>";

}

$out .= "\n</ul>";

return $out;

}

//parameters: current page, menu depth, ul menu id

$menu = treeMenu($pages->get("/"), 2, "myMenu");

echo $menu;

Link to comment
Share on other sites

If it's still relevant. I took Apeisa's code from the above-mentioned topic and modified it a bit and it seems to work the way you wanded, Soma :)

<?php

function treeMenu(Page $page = null, $depth = 1, $id = null) {

           $depth -= 1;

           if(is_null($page)) $page = wire('page');
           if(!is_null($id)) $id = " id='$id'";

           $out = "\n<ul$id>";

           $parents = $page->parents;

           // This is where we get pages we want. You could just say template!=news-item
           foreach($page->children() as $child) {
                   $class = "level-" . count($child->parents);
                   $s = '';
                   if($child->numChildren && $depth > 0 ) {
                           $s = str_replace("\n", "\n		", treeMenu($child, $depth));
                   }

                   $class .= " page-{$child->id}";
                   $class = " class='$class'";
                   $out .= "\n	<li$class>\n		<a$class href='{$child->url}'>{$child->title}</a>$s\n	</li>";
           }
       $out .= "\n</ul>";

       return $out;
}

//parameters: current page, menu depth, ul menu id
$menu = treeMenu($page, 3, "myMenu");

echo $menu;

You shoud pass depth as a second parameter. By default it will output only one level.

Edit: optimized code a bit

I like this code, but i need the "Home" menu to be always included...thanks..please help..

Link to comment
Share on other sites

Welcome newbie :)

I did a small change to the code, so it includes the self page on the tree.

EDIT: the code I posted wasn't working. Using the same code you posted here, just change the end (after the function closes) to this:

$homepage = $pages->get("/");

$menu = treeMenu($homepage, 3, "");

echo "<ul id='myMenu'>";
echo "<li class='level0'><a class='level0' href='{$homepage->url}'>{$homepage->title}</a>";
echo $menu;
echo "</li></ul>";

This is not a very elegant way of doing this. Would be best to change the function. But it works.

Edited by diogo
Link to comment
Share on other sites

hi drogo..thanks for discussing with me...

Home About Media Bills & Resolutions Publications Projects Gallery Contact Us

thats what i want..even if i will go to "about" page or other page, the navigation structure will still looks the same...thanks

Link to comment
Share on other sites

I'm still a bit confused with what you need... still, another try:

function treeMenu(Page $page = null, $depth = 1, $id = null) {

   $depth -= 1;

   if(is_null($page)) $page = wire('page');
   if(!is_null($id)) $id = " id='$id'";

   $out = "\n<ul$id>";
   $out .= "\n	 <li class='level-1'>\n			<a  class='level-1' href='{$homepage->url}'>{$homepage->title}</a>\n   </li>"; // added the homepage here					  

   $parents = $page->parents;

   // This is where we get pages we want. You could just say template!=news-item
   foreach($page->children() as $child) {
       $class = "level-" . count($child->parents);
       $s = '';
       if($child->numChildren && $depth > 0 ) {
           $s = str_replace("\n", "\n			  ", treeMenu($child, $depth));
       }

       $class .= " page-{$child->id}";
       $class = " class='$class'";
       $out .= "\n	 <li$class>\n			<a$class href='{$child->url}'>{$child->title}</a>$s\n   </li>";
   }
   $out .= "\n</ul>";

   return $out;
}

$homepage = $pages->get("/");

//parameters: current page, menu depth, ul menu id
$menu = treeMenu($homepage, 2, "myMenu"); // this will make the menu be the same in any page

echo $menu;
Link to comment
Share on other sites

Thank you very much Soma...but i only need to display the treemenu up to the 2nd level only at th homepage page...tnx

That's what can be done with this module pretty easily.

if($page->template == "home"){
$options = array(
	'max_levels' => 2,
	'show_root' => true
);

echo $treeMenu->render($options);
}

https://github.com/somatonic/MarkupSimpleNavigation/wiki/MarkupSimpleNavigation-Documentation

Link to comment
Share on other sites

Thank you very much Soma and drogo for your help..i've already modified the simpleNavigation module to display what i want....thanks more power...

Thanks again Soma, that's exactly what i did in the module...forgive me im weak in php...hehehe....thanks

Link to comment
Share on other sites

Thank you very much Soma and drogo for your help..i've already modified the simpleNavigation module to display what i want....thanks more power

What did you modify if I might ask? :)

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