Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function: multi-level navigation. / Plugin for version 2!
#1
USING GETSIMPLE VERSION 2.0 AS YOU SHOULD?
STILL WANT TO USE MY MULTI-LEVEL NAVIGATION?
GET THE PLUGIN — READ MORE.

As this seems to be a problem I shared with many others I thought it might be nice to publish my solution.

The problem is simple to explain. GetSimple offers a way to assign pages to certain parents, but the menu function doesn't built the menu accordingly. With this function it will!

I have to point out that I'm a little ill and wrote this within the last hour to throw into my own template, so please tell me if you notice any faults!

How to use it.

Really easy, just call this function instead of the standard one that comes with GetSimple. The standard get_navigation() is normally used as followed:
Code:
get_navigation(return_page_slug());
My function is used like this:
Code:
menu_master();
You will find the function at the bottom of this post, just put it in your template's functions.php.

What HTML does it generate?

Very important to know once you start adding CSS to spruce it up. Here is the HTML generated when I'm on subpage-2. I think this is nice and clear HTML for generic use. The HTML outputted by the function does not include any white space at all!
Code:
<ul class="menu">
  <li>
    <a href="http://example.com/">Home</a>
  </li>
  <li class="parent">
    <a href="http://example.com/page-1">First page</a>
    <ul class="submenu">
      <li>
        <a href="http://example.com/page-1/subpage-1">First sub-page</a>
      </li>
      <li class="active">
        <a href="http://example.com/page-1/subpage-2">Second sub-page</a>
      </li>
      <li>
        <a href="http://example.com/page-1/subpage-3">Third sub-page</a>
      </li>
    </ul>
  </li>
  <li>
    <a href="http://example.com/page-2">Second page</a>
  </li>
  <li>
    <a href="http://example.com/page-3">Third page</a>
  </li>
  <li>
    <a href="http://example.com/page-4">Fourth page</a>
    <ul class="submenu">
      <li>
        <a href="http://example.com/page-4/subpage-4">Fourth sub-page</a>
      </li>
      <li>
        <a href="http://example.com/page-4/subpage-5">Fifth sub-page</a>
      </li>
    </ul>
  </li>
  <li>
    <a href="http://example.com/page-5">Fifth page</a>
  </li>
</ul>

Edits.

2009-11-16 22:40 (GMT+1):
  • Fixed the fact that li-elements were not being closed.
  • Removed a double semi-colon on one of the lines.
  • Added extra classes, see the example HTML. [Requested by Texta]
2009-11-18 16:56 (GMT+1)
  • Fixed li-elements being closed before the submenu is added. [Pointed out by Patricia]
2009-11-18 20:54 (GMT+1)
  • Removed subval_sort(). [Adviced by n00dles101]

The function as is.

I provide this function as it is. I will probably update it when a GetSimple update breaks it or when others come up with suggestions, but I won't promise that. If anything suddenly wipes your system clean when you use this, don't look at me either.
Code:
function menu_master() {
        $data = simplexml_load_string(menu_data('',true));
        $menu = $data->xpath('//*[menuStatus="Y"][parent=""]');
        if (count($menu)>0) {
            echo '<ul class="menu">';
            foreach ($menu as $link) {
                if ("$link->slug"=="") $link->slug = "index";
                $prnt = count($data->xpath('//item[slug="'.return_page_slug().'"][parent="'.$link->slug.'/"]'));
                echo '<li'.("$link->slug"==return_page_slug()?' class="active"':($prnt>0?' class="parent"':'')).'><a href="'.$link->url.'">'.($link->menu!=""?$link->menu:$link->title).'</a>';
                $menu = $data->xpath('//*[menuStatus="Y"][parent="'.$link->slug.'/"]');
                if (count($menu)>0) {
                    echo '<ul class="submenu">';
                    foreach ($menu as $link) {
                        echo '<li'.("$link->slug"==return_page_slug()?' class="active"':'').'><a href="'.$link->url.'">'.($link->menu!=""?$link->menu:$link->title).'</a></li>';
                    }
                    echo '</ul>';
                }
                echo '</li>';
            }
            echo '</ul>';
        }
    }
“Don’t forget the important ˚ (not °) on the a,” says the Unicode lover.
Help us test a key change for the core! ¶ Problems with GetSimple? Be sure to enable debug mode!
Reply


Messages In This Thread
Function: multi-level navigation. / Plugin for version 2! - by Zegnåt - 2009-11-17, 03:34:23



Users browsing this thread: 2 Guest(s)