Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add category links as sub-menus to Perch CMS navigation
<!--*
// ====================================================================
// CATEGORY SUBMENU
// ====================================================================
// outputs set of category links for sub-menu (/perch/templates/categories/custom/category_submenu.html)
// called by perch_categories() in nav.php
*-->
<perch:before><ul class="sub-menu"></perch:before>
<li<perch:if id="catDepth" match="gt" value="1"> class="indent"</perch:if> ><a href="/<perch:category id="catPath" />"><perch:category id="catTitle" type="smarttext" /></a></li>
<perch:after></ul></perch:after>
<!--*
// ====================================================================
// MAIN MENU LIST ITEMS
// ====================================================================
// this is the primary navigation item template (/perch/templates/navigation/custom/item.html)
// called by perch_pages_navigation();
*-->
<perch:before><nav id="nav" class="nav" role="navigation" aria-labelled-by="nav2">
<p><a class="skip" href="#menu">Menu</a></p>
<ul id="menu">
</perch:before><li class="<perch:if exists="subitems">has-subitems</perch:if><perch:if id="pagePath" match="in" value="/where-to-stay, /walking">has-subitems</perch:if><perch:if exists="current_page"> selected</perch:if><perch:if exists="ancestor_page"> ancestor</perch:if>"><a href="<perch:pages id="pagePath" />"><perch:pages id="pageNavText" /></a><perch:pages id="subitems" encode="false" />
<perch:if id="pagePath" match="eq" value="/where-to-stay"><perch:pages id="cats_accommodation" encode="false" /></perch:if>
<perch:if id="pagePath" match="eq" value="/walking"><perch:pages id="cats_walks" encode="false" /></perch:if>
</li><perch:after>
</ul>
</nav></perch:after>
<?php
// ====================================================================
// MAIN MENU NAVIGATION
// ====================================================================
// This outputs sets of category links as sub-menus in the navigation template when matched to specific pagePath id
//
// save set of category links for sub menu
$cats_accommodation = perch_categories([
'set'=> 'where-to-stay',
'template' => 'custom/category_submenu.html'
], true);
// another category set
$cats_walks = perch_categories([
'set'=> 'walking',
'template' => 'custom/category_submenu.html'
], true);
// save sub-menu links as system variables so they can be referenced by id in the navigation template
PerchSystem::set_vars([
'cats_accommodation' => $cats_accommodation,
'cats_walks' => $cats_walks,
]);
// output main nav
perch_pages_navigation([
'template' => ['custom/item.html', 'custom/subitem.html'],
'hide-extensions' => 'true',
]);
?>
<!--*
// ====================================================================
// SUB MENU LIST ITEMS
// ====================================================================
// this is the secondary / sub-menu navigation item template (/perch/templates/navigation/custom/subitem.html)
// called by perch_pages_navigation();
*-->
<perch:before><ul class="sub-menu"></perch:before>
<li<perch:if exists="current_page"> class="selected"</perch:if>><a href="<perch:pages id="pagePath" />"><perch:pages id="pageNavText" /></a>
</li>
<perch:after></ul></perch:after>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.