newbie140 Posted June 4, 2012 Share Posted June 4, 2012 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 More sharing options...
newbie140 Posted June 5, 2012 Share Posted June 5, 2012 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 More sharing options...
diogo Posted June 5, 2012 Share Posted June 5, 2012 (edited) 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 June 5, 2012 by diogo Link to comment Share on other sites More sharing options...
newbie140 Posted June 6, 2012 Share Posted June 6, 2012 Thanks very much diogo...but it doesnt work the way i want it...my point is to include the "Home" menu together with other menus...thanks for your help Link to comment Share on other sites More sharing options...
diogo Posted June 6, 2012 Share Posted June 6, 2012 I'm not sure if i understand. What do you mean by together? Can you put a scheme of what you want here? Link to comment Share on other sites More sharing options...
newbie140 Posted June 6, 2012 Share Posted June 6, 2012 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 More sharing options...
Soma Posted June 6, 2012 Author Share Posted June 6, 2012 Sorry to disrupt you discussion. Why not simply use https://github.com/somatonic/MarkupSimpleNavigation ? Link to comment Share on other sites More sharing options...
newbie140 Posted June 6, 2012 Share Posted June 6, 2012 Thank you very much Soma...but i only need to display the treemenu up to the 2nd level only at th homepage page...tnx Link to comment Share on other sites More sharing options...
diogo Posted June 6, 2012 Share Posted June 6, 2012 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 More sharing options...
Soma Posted June 6, 2012 Author Share Posted June 6, 2012 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 More sharing options...
newbie140 Posted June 6, 2012 Share Posted June 6, 2012 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 More sharing options...
Soma Posted June 6, 2012 Author Share Posted June 6, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now