Parent submenu (list children on child page)
- bayerberg
- Master Bludit
- Posts: 143
- Joined: Wed Jun 07, 2017 1:05 pm
- Location: London, UK
- Has thanked: 10 times
- Been thanked: 11 times
- Contact:
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
Parent
- childPage
- childPage
- childPage
- bayerberg
- Master Bludit
- Posts: 143
- Joined: Wed Jun 07, 2017 1:05 pm
- Location: London, UK
- Has thanked: 10 times
- Been thanked: 11 times
- Contact:
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/>';
?>
- multicolordev
- Master Bludit
- Posts: 137
- Joined: Thu May 26, 2022 12:33 pm
- Has thanked: 15 times
- Been thanked: 91 times
- Misteric
- Ssr. Bludit
- Posts: 16
- Joined: Mon Aug 08, 2022 2:55 pm
- Has thanked: 6 times
- Been thanked: 6 times
Hi,
A Bludit (v3.14) script for a bootstrap menu of the parents with dropdown of the child pages:
Greets,
Misteric
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>';
Misteric
-
- Jr. Bludit
- Posts: 3
- Joined: Tue Aug 15, 2023 12:39 am
- Location: Texas
- Has thanked: 3 times
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.
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>';
}
}
?>