Nested Menus

Downloads: 10267
Category: Plugin
Compatible with: GetSimple 3.0 - 3.0

Last Updated: February 21, 2012
Tags: nested menu list parent children child submenu navigation subnav subnavigation nav expanding
Author: chrisbloom7
Author Website: http://www.christopherbloom.com/
Support: Support Forum

(4.3) 20 Votes

Plugin Description:

Provides a set of template tags to return or print the main navigation as a set of nested pages, i.e. child items under parent. Will return only public items that are set to be displayed in the menu.

Example

Suppose you have a set of pages defined in the admin area like so:

  • Page 1
  • Page 2
    • Sub Page 1
    • Sub Page 2
  • Page 3
  • Page 4
    • Sub Page 3

For simplicity sake, let's assume that these are all public pages, set to display in the menu, with their menu prioritization in the same order.

Using the normal menu functions all of those pages would be displayed as a flat unordered list:

<li class="current  index"><a href="/" title="Page 1">Page 1</a></li>
<li class="page-2"><a href="/index.php?id=page-2" title="Page 2">Page 2</a></li>
<li class="page-2 sub-page-1"><a href="/index.php?id=sub-page-1" title="Sub Page 1">Sub Page 1</a></li>
<li class="page-2 sub-page-2"><a href="/index.php?id=sub-page-2" title="Sub Page 2">Sub Page 2</a></li>
<li class="page-3"><a href="/index.php?id=page-3" title="Page 3">Page 3</a></li>
<li class="page-4"><a href="/index.php?id=page-4" title="Page 4">Page 4</a></li>
<li class="page-4 sub-page-3"><a href="/index.php?id=sub-page-3" title="Sub Page 3">Sub Page 3</a></li>

Using Nested Menus, we end up with this instead:

<li class="current index first"><a href="/" title="Page 1">Page 1</a></li>
<li class="page-2 submenu"><a href="/index.php?id=page-2" title="Page 2">Page 2</a>
  <ul>
    <li class="sub-page-1 first"><a href="/index.php?id=sub-page-1" title="Sub Page 1">Sub Page 1</a></li>
    <li class="sub-page-2 last"><a href="/index.php?id=sub-page-2" title="Sub Page 2">Sub Page 2</a></li>
  </ul>
</li>
<li class="page-3"><a href="/index.php?id=page-3" title="Page 3">Page 3</a></li>
<li class="page-4 submenu last"><a href="/index.php?id=page-4" title="Page 4">Page 4</a>
  <ul>
    <li class="page-4 sub-page-3 first last"><a href="/index.php?id=sub-page-3" title="Sub Page 3">Sub Page 3</a></li>
  </ul>
</li>

Note

This plugin currently only supports 1 level of nesting. Pages nested deeper than that will be ignored.

Change log

Version 1.4 (Released 9-Nov-2011)

  • Fixed bug that would cause pages not marked for display in the nav to appear anyway
  • Added $echo argument to the getnestednavigation function. It is true by default. Setting it to false will cause the function to return the menu HTML as a string instead of outputting it directly.
  • Added an event trigger to clear the cache after a page is deleted.
  • Added a check to make sure that a parent page is set to be displayed in the nav before adding any children to it. This should also prevent pages nested deeper than 1 level from mucking up the array.

Install Instructions:

Extract the nested_menus.php file to your plugins directory. Then in your template you can use

<?php get_nested_navigation(); ?>

in place of

<?php get_navigation(); ?>

If you want to customize the output of the nested menu data, use

<?php $menu_data = nested_menu_data(); ?>

in place of

<?php $menu_data = menu_data(); ?>

Since version 1.4 you can also get the formatted menu HTML as a string using the syntax

<?php $menu_html = get_nested_navigation($echo = false); ?>

Archived Versions:

These are old versions of this file. Please use with caution as they are probably out of date and no longer supported. ShowHide