Jump to content

Coding A Menu That Includes The Parent And Children Pages


Travo
 Share

Recommended Posts

Hello, I return for further assistance!

I am currently facing an issue where I want to code something as simple as a menu, but fail to do so without it losing its flexibility (I don't want to hardcode any ID-s or names into it).

I'll keep it short:

The first menu item is the parent, all the others are its children. How would I go about retrieving the parent and its children information even when I navigate between the two?

My current code is basically the same as the default one: retrieve children and add the parent as the first array item. And obviously, this breaks when I open the parent page.

$children = $page->parent->children;
$children->prepend($page->parent);

foreach($children as $child) {
		$class = $child === $page ? " active" : '';
        echo "<li  class='sidesub$class'><a href='{$child->url}'>{$child->title}</a></li>";
}
Link to comment
Share on other sites

The easiest way would be to hardcode the parents id. There's no real way around some form of hardcoding, as from some source the code has to know, which site should be your root page. If you want the menu to remain slightly more dynamic you could use a page-field, where you could change the parent page from the backend.

If your mentioned parent page also is the homepage, then you could use this to get the page:

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

This always selects the root of the processwire pagetree.

  • Like 1
Link to comment
Share on other sites

I hardcoded the page name and ended up with this, works fine:

<?php
$alalehed = $pages->get("/andmebaas/")->children;

if ($alalehed->get("id=$page")) {
	$children = $page->children;
	$children->prepend($page);
} else {
	$children = $page->parent->children;
	$children->prepend($page->parent);
}

foreach($children as $child) {
		$class = $child === $page ? " active" : '';
        echo "<li  class='sidesub$class'><a href='{$child->url}'>{$child->title}</a></li>";
}
?>
Link to comment
Share on other sites

You don't need the if statement, as it doesn't matter on which page you are. The menuitems are in each case the same.

<?php
$alalehed = $pages->get("/andmebaas/");

$children = $alalehed->children;
$children->prepend($alalehed);

foreach($children as $child) {
		$class = $child === $page ? " active" : '';
        echo "<li  class='sidesub$class'><a href='{$child->url}'>{$child->title}</a></li>";
}
?>
  • Like 1
Link to comment
Share on other sites

Thanks for your contribution, but you're wrong. I'll write it out as page locations.

First menu item acts as the 'intro page'.

Main/Andmebaas/Bicycles
Main/Andmebaas/Bicycles/Roadbikes
Main/Andmebaas/Bicycles/Mountainbikes
...

In order to get the first page to display everything correctly I have to get its children pages directly ($page->children). If I am on the following pages (Roadbikes for example), I have to gets its parents' (Bicycles) children ($page->parent->children).

I hope you understand. :)

Link to comment
Share on other sites

if you have the intro-pages have a distinct template you could do something like

if($page->parent->is("template=intro-page")){ // or $page->parent->is("intro-page")
    $children = $page->siblings->prepend($page->parent);
} else {
    $children = $page->children->prepend($page);
}

Or if you know the level you could do for example:

switch($page->parents->count){
    case 1:
        $children = $page->children->prepend($page);
        break;
    case 2:
        $children = $page->siblings->prepend($page->parent);
        break;
}
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...