Jump to content

List child pages


kaz
 Share

Recommended Posts

I need to list subpages by mengtest and title of a mother page as a simple foreach ul list. Is there a way, similar to a menu, to list child pages? I couldn't find anything about this in the docs. Maybe you can give me a hint what to look for?

Link to comment
Share on other sites

If you know the parent page you can iterate over all the children very easily, even passing in a selector with your mengtest condition. You could try...

echo "<ul>";
foreach($page->children('mengtest=...') as $child) {
  echo "<li><a href='$child->url'>$child->title</a></li>";
}
echo "</ul>";

Docs: https://processwire.com/api/ref/page/children/

  • Like 1
Link to comment
Share on other sites

I was looking at this thread earlier and was confused, but now I’ve had a drink, I think you want to show a single list of pages whose parent matches some conditions?

You can use selectors on fields of the parent like this:

pages()->find('parent.title=something, parent.mengtest=orother')

 

Link to comment
Share on other sites

@netcarver thanks netcarver, this helped a lot.

echo "<ul id='side-menu' class='uk-list uk-margin-remove uk-padding-remove'>";
foreach($pages->get(1044)->children as $child) {
    $active = wire('page') == $child ? "current" : "";
    echo "<li class='$active'><a href='$child->url' title='$child->title'>" . $child->get('menutext|title') . "</a></li>";
}
echo "</ul>";

I call the list pages in my base template with the page ID of the parent page:

<?php if ($page->id == 1044): ?>
<?php include('views/partials/menu-aside.php'); ?>
<?php endif; ?>

However, the list should also be displayed in all children's pages. ID is therefore wrong? Is there another option which includes the children's sites?

Link to comment
Share on other sites

1 hour ago, netcarver said:

I might be misunderstanding, but couldn't you do this in the base template...?

Exactly, that was my goal, to do it in the base template. Thanks that works!

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