Hi again,
Another beginner PHP question, using Kirby 4 and ZOON theme.
In ZOON, you can set a sub-menu to show as a tree menu at the right side of the page, which is very handy. However, when there is a collection
of sub-pages involved, i.e. a blog, it becomes very long and unsightly.
Hence I want the tree menu at the side to only work when it’s children/sub-pages of an article
and not a collection
, as I tend to only use collections
for blogs.
site/snippets/submenu.php
is:
<nav class="submenu">
<h2 class="submenu-heading">
<a href="<?= $section->url() ?>">
<?= $section->title() ?>
</a>
</h2>
<?php
$subpages = $pages->findOpen()->children();
snippet('treemenu', ['subpages' => $subpages]);
?>
</nav>
And site/snippets/treemenu.php
is:
<?php if (!isset($subpages)) $subpages = $site->pages() ?>
<ul>
<?php foreach ($subpages->listed() as $p) : ?>
<li class="depth-<?= $p->depth() ?>">
<a class="<?= e($p->isActive(), 'current', '') ?> <?= e($p->hasChildren(), 'children', '') ?>" <?php e($p->isActive(), 'aria-current="page"') ?> href="<?= $p->url() ?>">
<?= $p->title()->html() ?> <span class="submenu-triangle" role="presentation"><?= ($p->hasChildren()) ? 'ˇ' : '' ?></span>
</a>
<?php if ($p->hasChildren() && $p->isOpen()) {
snippet('treemenu', ['subpages' => $p->children()]);
} ?>
</li>
<?php endforeach ?>
</ul>
I’ve looked at Do not show subpages in specific main menu item and the line that stands out to me as being relevant is:
<?php if ($children->isNotEmpty() && $item->is(page('products')) === false): ?>
I figure the line I need to modify is the following in site/snippets/treemenu.php
:
<?php if (!isset($subpages)) $subpages = $site->pages() ?>
So I tried:
<?php if (!isset($subpages)) $subpages = $site->pages() && $section->template() != 'collection'; ?>
Doesn’t break anything, but doesn’t work either, I must be getting the syntax wrong.
If it helps, this is my site structure:
Main page
- The Garden (
article
, submenu navigation turned on)- Garden blog (
collection
: where I post and content is ordered chronologically) - Formats (
article
, where content is sorted by format, has sub-pages) - Themes (
article
, where content is sorted thematically, has sub-pages)
- Garden blog (
Here are some screenshots to illustrate:
The tree menu is helpful for the other pages and their sub-pages, because I use it to organize my content:
However, where there is a collection
, it becomes a problem.
Thank you once again for any help!