Forum

Thread tagged as: Question, Configuration

Can I add a set of categories as second level navigation items?

Is it possible to have a main navigation somewhere on the page and list a set of categories as dropdown submenu for one of the pages in the mainmenu?

Robert Schweizer

Robert Schweizer 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

As part of the perch_pages_navigation() output, you mean?

Yeah, exactly.

Simon Clay

Simon Clay 127 points

I think you should be able to do it in the same way as I did for a multi item region containing products.

On the main page to show the nav:

<?php 

//find products and save them into a variable $product_list
$product_list = perch_content_custom('Product Details', array(
  'page' => '/our-range/products.php',
  'template' => '_products_in_nav.html',
  'include-parent' => true,
), true); 

//pass $product_list into PerchSystem::set_var for use in the nav level2.html template
PerchSystem::set_var('productlist', $product_list);

//call the nav with two templates, one for top level and one for dropdown
perch_pages_navigation(array(
  'template' => array('level1.html', 'level2.html'),
  'hide-extensions' => 'true',
));
?>

Here is the _products_in_nav.html template:

<li>
    <a href="/our-range/<perch:content id="slug" type="slug" />"><perch:content id="product_title" type="text" /></a>
</li>

and here is the level2.html submenu template:

<perch:before>
    <ul class="subMenu">
</perch:before>
        <li>
            <a href="<perch:pages id="pagePath" />"><perch:pages id="pageNavText" /></a>
            <perch:pages id="subitems" encode="false" />


        </li>
        <perch:if id="pagePath" match="eq" value="/our-range/ingredients"> <!--check if the path matches where I want the products to go and if so output the variable 'productlist' set in the main page-->
            <perch:pages id="productlist" encode="false" />
        </perch:if>
<perch:after>
    </ul>
</perch:after>

The main bit you'd need to change for your requirement is the very first bit of code to set the list of categories as a variable to pass into the nav.

I hope that helps.