Jump to content

rekursive menu, prepend home


ngrmm
 Share

Recommended Posts

how do i prepend home (root) to my rekursive menu
this is the original code from ryan

<?php
function treeMenu(Page $page = null, Page $rootPage = null) {

        if(is_null($page)) $page = wire('page');
        if(is_null($rootPage)) $rootPage = wire('pages')->get('/');

        $out = "\n<ul>";
        $parents = $page->parents;

        foreach($rootPage->children as $child) {
                $class = "level-" . count($child->parents);
                $s = '';

                if($child->numChildren && $parents->has($child)) {
                        $class .= " on_parent";
                        $s = str_replace("\n", "\n\t\t", treeMenu($page, $child));

                } else if($child === $page) {
                        $class .= " on_page";
                        if($page->numChildren) $s = str_replace("\n", "\n\t\t", treeMenu($page, $page));
                }

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

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

echo treeMenu(); 
?>   

 

Link to comment
Share on other sites

Try to replace the line

$out = "\n<ul>";

with this one:

$out = "\n<ul>\n\<li>\n\t\t<a href='{$rootPage->url}'>{$rootPage->title}</a>\n\t</li>";

(adding a class to the a element and modifying the \t tabs as needed).

  • Like 1
Link to comment
Share on other sites

50 minutes ago, ottogal said:

Try to replace the line


$out = "\n<ul>";

with this one:


$out = "\n<ul>\n\<li>\n\t\t<a href='{$rootPage->url}'>{$rootPage->title}</a>\n\t</li>";

(adding a class to the a element and modifying the \t tabs as needed).

thx ottogal
do you know how i could get rid of prepanding the parent through all levels? i just need it on the first level (Home-Button).

Link to comment
Share on other sites

19 hours ago, ngrmm said:

do you know how i could get rid of prepanding the parent through all levels?

Of course it wouldn't work like I proposed since the function treeMenu() is called recursively...

So better try kongondo's example.

 

Or try to put the wrapping ul tag out of the function:

<?php
function treeMenu(Page $page = null, Page $rootPage = null) {

        if(is_null($page)) $page = wire('page');
        if(is_null($rootPage)) $rootPage = wire('pages')->get('/');

        $tree = "";
        $parents = $page->parents;

        foreach($rootPage->children as $child) {
                $class = "level-" . count($child->parents);
                $s = '';

                if($child->numChildren && $parents->has($child)) {
                        $class .= " on_parent";
                        $s = str_replace("\n", "\n\t\t", treeMenu($page, $child));

                } else if($child === $page) {
                        $class .= " on_page";
                        if($page->numChildren) $s = str_replace("\n", "\n\t\t", treeMenu($page, $page));
                }

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

        return $tree;
}

$out = "\n<ul>\n\<li>\n\t\t<a href='{$rootPage->url}'>{$rootPage->title}</a>\n\t</li>";
$out .= treeMenu();
$out .= "\n</ul>";

echo $out; 

Note that I changed the local variable $out in the function to $tree. 

(I didn't test that myself...)

Link to comment
Share on other sites

How about removing starting and ending UL's from the function and add it manually with the homepage link, like this (browser code!):

$menu = '<ul>' . '<li><a href="' . {$rootPage->url} .'">' . {$rootPage->title} . '</a> . '</li>' . treeMenu() . '</ul>';

 

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