Jump to content

Need a little help showing if > children in my menu.


OrganizedFellow
 Share

Recommended Posts

For my simple primary navigation, I use this:

$root = $pages->get("/");
$children = $root->children();
$children->prepend($root);
foreach($children as $child) {
    echo "<li><a href='{$child->url}'>{$child->title}</a></li>\n";
}

It outputs an unordered list horizontally at the top of my page.

Wonderful.

Some of my pages have children:

About > About Our Company

About > About Me

Contact > Contact Me

Contact > Contact The Other Guy

Contact > Contact The Girl

Browse > By Price

Browse > By Size

Browse > By Artist

I thought I should use something like the above code but change where it gets root. So, instead of get site root, make it get root of about, or root of contact, root browse. But I'm sure it can be better written.

$page=$page->name;
if ($page=="browse") {
    $root = $pages->get("/browse");
    $children = $root->children();
    foreach($children as $child) {
        echo "<li><a href='{$child->url}'>{$child->title}</a></li>\n";
    }
}elseif ($page=="about") {
    $root = $pages->get("/about");
    $children = $root->children();
    foreach($children as $child) {
        echo "<li><a href='{$child->url}'>{$child->title}</a></li>\n";
    }
}

What if I add more root pages that have children? I shouldn't have to hard code it, but I just haven't figured that out.

What if later the client wants to add a summary or change something above the UL?

  • Like 1
Link to comment
Share on other sites

I’m not exactly sure if that’s what you want, but if you just want to generate a bunch of nested <ul>s, you should recursively travel around the page tree. Something like:

function siteMap(Page $root)
{
	$out = "<li>{$root->title}";
	
	if ($root->hasChildren) {
		$out .= '<ul>';
		foreach($root->children() as $child) {
			$out .=  siteMap($child);
		}
		$out .= '</ul>';
	}
	
	$out .= '</li>';
	
	return $out;
}

Uuuuh… at least I think that should work. Untested :P

It should give you the complete branch starting at whatever you plug into $root.

edit: Actually. Something is bound to be wrong with this. And you need to supply the outer <ul> yourself.

edit: Tested it. Fixed it because it was stupid.

  • Like 2
Link to comment
Share on other sites

There are different solutions depending on which page you want to use as the root of your navigation:

1. You want to use the root page

You use the solution that you have given.

2. You want to use the pages just under the root page

You define the root of the navigation using $page->rootParent, then loop over its children:

$root = $pages->get("/");
$navigationRoot = $page->rootParent;
$children = $navigationRoot->children();

// Uncomment the following if you want to keep a link to the home page
//$children->prepend($root);

foreach($children as $child) {
    echo "<li><a href='{$child->url}'>{$child->title}</a></li>\n";
}

I sometimes use this to do a secondary navigation within a section.

3. You want to use pages with the same template

This is very useful to create a subnavigation. For example, imagine you have a structure like this:

... (Any kind of structure above)
-- Category 1
---- Subcategory 1.1
------ Product 1.1.1
------ Product 1.1.2
---- Subcategory 1.2
------ Product 1.2.1
...etc.

It is often useful to do a subnavigation of the categories when you are on a Product page. The way you could do this is like this:

$navigationRoot = $page->parent("template=category")->parent;
$categories = $navigationRoot->children("template=category");
foreach($categories as $category) {
    echo "<li><a href='{$category->url}'>{$category->title}</a></li>\n";
}

If you want to show the subcategories too, you can loop across $category->children.

In all these cases, you can go any level deeper by using Jan's technique (and possibly using a counter to limit the depth).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...