Page 1 of 1
Parent submenu (list children on child page)
Posted: Sat Jun 17, 2023 4:33 pm
by bayerberg
What's the quickest method of listing siblings of a static page? I have a static parent page and want to create a menu that shows only parent + child pages. Something like this
Parent
- childPage
- childPage
- childPage
Re: Parent submenu (list children on child page)
Posted: Sat Jun 17, 2023 4:39 pm
by bayerberg
aaand looks like i did it!
Code: Select all
<?php
echo '<hr/><nav><ul>';
$children = $page->parentMethod('children');
foreach ($children as $child) {
echo '<li>» <a href="'.$child->permalink().'">'.$child->title().'</a></li>';
}
echo '</ul></nav><hr/>';
?>
Re: Parent submenu (list children on child page)
Posted: Sat Jun 17, 2023 7:52 pm
by multicolordev
navX or MultiMenu plugin:) if you want use plugins for next project!
Re: Parent submenu (list children on child page)
Posted: Fri Jul 07, 2023 7:51 am
by Misteric
Hi,
A Bludit (v3.14) script for a bootstrap menu of the parents with dropdown of the child pages:
Code: Select all
echo '<div class="dropdown">';
// Get the list of parent pages
$parents = $staticContent;
foreach ($parents as $parent) {
if(!$parent->isChild() && $parent->hasChildren()) {
echo '<a class="btn dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">';
echo $parent->title();
echo '</a>';
} else if ($parent->isParent()) {
echo '<a class="btn" href="'.$parent->permalink().'">';
echo $parent->title();
echo '</a>';
}
// Check if the page has children
if ($parent->hasChildren()) {
// Get the list of children
$children = $parent->children();
echo '<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">';
echo '<li><a class="dropdown-item" href="'.$parent->permalink().'">';
echo $parent->title();
echo '</a></li>';
foreach ($children as $child) {
echo '<li><a class="dropdown-item" href="'.$child->permalink().'">';
echo $child->title();
echo '</a></li>';
}
echo '</ul>';
}
}
echo '</div>';
Greets,
Misteric
Re: Parent submenu (list children on child page)
Posted: Tue Feb 06, 2024 1:05 am
by GracefulForm
Thanks to the help of the previous messages, here is a complete snippet that will show a menu of child pages, whether you are viewing the parent page or one of the child pages. If you are on a page that has no child pages, there is a link back to the blog.
This will be helpful to those who are making a book with chapters or a documentation page with sections.
Code: Select all
<?php
$parents = $content;
foreach ($parents as $parent) {
// If current page is a parent, and has children, list the child pages.
if ($parent->hasChildren()) {
$children = $parent->children();
echo '<a href="'.$parent->permalink().'">'.$parent->title().'</a>';
echo '<br />';
echo '<br />';
echo 'Child Pages: <br/>';
foreach ($children as $child) {
echo '♦ <a href="'.$child->permalink().'">'.$child->title().'</a>';
}
// If current page is a child, list the children of the parent.
} elseif ($page->isChild()) {
echo '<a href="'.$page->parentMethod('permalink').'">'.$page->parentMethod('title').'</a>';
echo '<br />';
echo '<br />';
echo 'Child Pages: <br/>';
$children = $page->parentMethod('children');
foreach ($children as $child) {
echo '♦ <a href="'.$child->permalink().'">'.$child->title().'</a><br/>';
}
// If the parent page has no children.
} else {
echo '<a href="'.Theme::siteUrl().'">☚ Return to Blog</a>';
}
}
?>