OK Antti, I think I'm in the depressing stage at this point
In response to your suggestion here's my mutilated version of slkwrm's beautiful code; in which I'm trying to append a simple 'item-active' class on the current item (adding 'parent-active' on any parent's of the current item would be a bonus):
<?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>";
// This is where we get pages we want. You could just say template!=news-item or list the templates you do want
foreach($page->children() as $child) {
$class = "level-" . count($child->parents);
$s = '';
if($child->numChildren && $depth > 0 ) {
$s = str_replace("\n", "\n\t\t", treeMenu($child, $depth));
if ($child === $page) {
$class .= " current";
}
}
$class .= " page-{$child->id}";
$class = " class='$class'";
$out .= "\n\t<li$class>\n\t\t<a$class href='{$child->url}'>{$child->title}</a>$s\n\t</li>";
}
$out .= "\n</ul>";
return $out;
}
//parameters: current page, menu depth, ul menu id
$menu = treeMenu($page->rootParent, 4, "myMenu");
So from my reading of it and looking at the linked article in Ryan's post (
http://processwire.c...790.html#msg790) I'm guessing the argument to test if it should append the active class needs to be in the loop plus needs to work out when current page is same as $child?
Here's another attempt:
<?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>";
// This is where we get pages we want. You could just say template!=news-item or list the templates you do want
foreach($page->children() as $child) {
$class = "level-" . count($child->parents);
$s = '';
if($child->numChildren && $depth > 0 && $child->id === $page->$id) {
$class .= " current";
$s = str_replace("\n", "\n\t\t", treeMenu($child, $depth));
} else if ($child->numChildren && $depth > 0) {
$class .= "";
$s = str_replace("\n", "\n\t\t", treeMenu($child, $depth));
}
$class .= " page-{$child->id}";
$class = " class='$class'";
$out .= "\n\t<li$class>\n\t\t<a$class href='{$child->url}'>{$child->title}</a>$s\n\t</li>";
}
$out .= "\n</ul>";
return $out;
}
//parameters: current page, menu depth, ul menu id
$menu = treeMenu($page->rootParent, 4, "myMenu");
echo $menu;
If someone could sort out where I'm going wrong (I'm not even sure if it's my argument for detecting when page and child are the same or my placement of the arguments or if I'm just way off track) that would be much appreciated