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
#2
This will save those who wish to hand code there menus a lot of time by making the progress lots easier. I still don’t think it can beat an actually generated menu though.

Just for the record, it will not work on GetSimple versions before 2.01.
“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
#3
Zegnåt Wrote:I still don’t think it can beat an actually generated menu though.

Agreed, but as long as GS doesn't support more than two levels, you have to do some trick if you need a 3rd or 4rd one (and still want to use GetSimple ;-))

Quote:Just for the record, it will not work on GetSimple versions before 2.01.

Thank you, post edited.
An example of any function I used that's not supported by 2.0 or 1.x...?
Reply
#4
find_url() is very new and was not defined until 2.01.

Eg: my menu plugin (the version that is currently being beta tested) ships with an extra file to ensure backwards compatibility by defining find_url if it doesn’t exist.
“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
#5
I found a bug in the function, it didn't close the lists properly if last line is not a first level page.

Replaced:
Code:
echo '</li></ul>';
with:
Code:
for( $i=0; $i <= $thislevel; $i++) echo '</li></ul>';

I have just edited the function (now v1.1). Sorry.
Reply
#6
Can page anchors also be added to these menus?
I have a directory project, where each page contains multiple articles within a specific category.
I want to have category anchors as a menu option for each page in the directory.
Sort of like this...

Foods
#Fruits Page
##Grapes Article
##Melons Article
##Oranges Article

Is that possible?

Thanks.

homershines
Reply
#7
Sorry, this script does not support slugs with anchors, as is.

Only thing I can think may be done is inserting html code just like text labels. You could do something like:

Foods
$fruits
$$<a href="fruits/#Grapes Article">Grapes</a>
$$<a href="fruits/#Melons Article">Melons</a>
$$<a href="fruits/#Oranges Article">Oranges</a>

In the function, on both lines 6 and 7, '#' should be changed to '$' (or other character you prefer to use for indenting, other than <, >, =, space, ...)

This example is for fancy URLs. With the default structure, it would be:
$$<a href="index.php?id=fruits#Grapes Article">Grapes</a>
...
Reply
#8
Thanks will try to get onto it tonight.

I really like this component.

Thanks again.

homershines
Reply




Users browsing this thread: 1 Guest(s)