Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Tree Multi-level Menu (quick hack)
#1
A quick-and-dirty function to display multi-level menus.

Edit: it will not work on GetSimple versions before 2.01 (thanks Zegnåt)

Edit again: bugfix 1.1

1. Create a text file (e.g. menu1.txt) like this:
Code:
page-slug
#subpage-slug
##subsubpage-slug
###subsubsubpage-slug
No slug, text label
#another-subpage-slug
... Upload it to data/other

2. In your theme's functions.php (create it if does not exist) insert this function:
Code:
// Simple Tree Menu v1.1 by Carlos Navarro
function simple_tree_menu($menufilename){
        $menufile = file('data/other/'.$menufilename);
        $prevlevel = -1;
        foreach ($menufile as $thisline) {
            if (trim($thisline) != '') {
                $thislevel = substr_count($thisline,'#');
                $thisline = trim( str_replace('#','',$thisline) );
                $diff=$thislevel - $prevlevel;
                if ($diff == 1)
                    echo '<ul class="menulevel-'.$thislevel.'"><li>';
                else
                    if ($diff < 0) {
                        while ($diff < 0) {
                            echo '</li></ul>';
                            $diff++;
                        }
                    }                
                if ($diff==0)
                    echo '</li><li>';
                if (file_exists('data/pages/'.$thisline.'.xml')) {
                    $thisfile = @file_get_contents('data/pages/'.$thisline.'.xml');
                    $data = simplexml_load_string($thisfile);
                    if ($data->menu != '')
                        $thistitle = $data->menu; // display menu text if present
                    else
                        $thistitle = $data->title; // if not, display page title
                    echo '<a href="'.find_url($data->url,$data->parent).'">'.$thistitle.'</a>';
                }
                else {
                    echo $thisline; // if page doesn't exist, display string
                }
                $prevlevel = $thislevel;
            }
        }
        for( $i=0; $i <= $thislevel; $i++) echo '</li></ul>';
}

?>

3. In your theme's template.php (e.g. in the sidebar) insert this code:
Code:
<?php simple_tree_menu('menu1.txt'); ?>

That's it. Suggestions and corrections welcome.

Demo here

PS could be better to use a Component instead of text files, however this way you are able to include several different menus.
Reply


Messages In This Thread
Simple Tree Multi-level Menu (quick hack) - by Carlos - 2010-03-21, 22:48:21



Users browsing this thread: 1 Guest(s)