Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sub-Navigation Menu
#1
Hi,

I am trying to create a sub-navigation menu on my website which displays all the sub pages for the current page.

I am using the multi-level navigation plugin as a base for this plugin but have come across a slight problem.

The following code will correctly ouput all of the child pages for the selected parent page, however when i select one of these child pages I am struggling to get the menu to stay visible.

I am assuming it's something to do with the $menu variable not containing anything when a child page has been selected?

Code:
$menu = $data->xpath('//*[menuStatus=""][parent="'.$a.'"]');
        usort($menu,"menu_sorting");
        
        if (count($menu) > 0) {    
        
            echo "<ul class='submenu'>\n";
            
                    foreach ($menu as $link) {                
                        echo '<li'.("$link->slug"==$a?' class="active"':'').'><a href="'.$link->url.'">'.($link->menu!=""?$link->menu:$link->title).'</a></li>';
                    }
            
            echo "</ul>";  
        }

Any ideas how i can get this to output the sub menu when a child page has been selected also?

Thanks very much.
Reply
#2
That piece of code is specifically to display child elements of a certain page. The first line could be called the selector, as you can see it is only grabbing pages which have the parent set to $a.

If you are just feeding the current page slug into $a it will not work on a subpage, this is because there are no pages that have the subpage as parent. If you want to use this code snippet you’ll want to make some sort of check like:
  • If this page has no parent, put this page’s slug in $a.
  • If this page has a parent, put this page’s parent’s slug in $a.
I hope you get the gist of it.
“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:That piece of code is specifically to display child elements of a certain page. The first line could be called the selector, as you can see it is only grabbing pages which have the parent set to $a.

If you are just feeding the current page slug into $a it will not work on a subpage, this is because there are no pages that have the subpage as parent. If you want to use this code snippet you’ll want to make some sort of check like:
  • If this page has no parent, put this page’s slug in $a.
  • If this page has a parent, put this page’s parent’s slug in $a.
I hope you get the gist of it.

Thanks for your reply Zegnåt, I'm slightly confused as to what you mean.

Could you possibly provide an example?

Thanks very much..
Reply
#4
that exactly that I want to do ...
Please Help Sad
Reply
#5
caviar,

I left this out my project but i'm keen to get this working as it would be very useful.

Ill take a look within the next couple days to get this working.

If anyone else has this working please share.
Reply
#6
something to try

replace the plugin
zegnat-multilevelmenu.php

Code:
<?php

$thisfile=basename(__FILE__, ".php");
register_plugin(
    $thisfile,
    'ZegnÃ¥t’s выпадающее меню.',
    '2',
    'Martijn',
    'http://zegnat.net/',
    'Плагин помогает сделать выпадющее меню на сайте. <br /><a href="ajax/menu.html" rel="facybox">Что нужно для установки?</a>'
);
add_action('index-pretemplate','append_function');
function append_function() {

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */

    function ye_olde_data() {
        global $PRETTYURLS;
        global $SITEURL;
        $path = "data/pages";
        $dir_handle = @opendir($path) or die("Unable to open $path");
        $filenames = array();
        while ($filename = readdir($dir_handle)) { $filenames[] = $filename; }
        $count=0;
        $pagesArray = array();
        if (count($filenames) != 0) {
            foreach ($filenames as $file) {
                if (!($file == "." || $file == ".." || is_dir("data/pages/".$file) || $file == ".htaccess")) {
                    $thisfile = @file_get_contents('data/pages/'.$file);
                    $data = simplexml_load_string($thisfile);
                    if ($data->private != 'Y') {
                        $pagesArray[$count]['menuStatus'] = $data->menuStatus;
                        $pagesArray[$count]['menuOrder'] = $data->menuOrder;
                        $pagesArray[$count]['menu'] = $data->menu;
                        $pagesArray[$count]['parent'] = $data->parent;
                        $pagesArray[$count]['title'] = $data->title;
                        $pagesArray[$count]['url'] = $data->url;
                        $pagesArray[$count]['private'] = $data->private;
                        $pagesArray[$count]['pubDate'] = $data->pubDate;
                        $count++;
                    }
                }
            }
        }
        if (count($pagesArray) != 0) {
            $count = 0;
            $xml = '<?xml version="1.0" encoding="UTF-8"?><channel>';    
            foreach ($pagesArray as $page) {
                if ($PRETTYURLS == '1') {
                    $href = $SITEURL.@($page['parent']!=''?tsl($page['parent']):'').($page['url']=='index'?'':$page['url']);
                } else {
                    $href = $SITEURL.($page['url']=='index'?'':'index.php?id='.$page['url']);
                }
                $xml.="<item>";
                $xml.="<slug><![CDATA[".$page['url']."]]></slug>";
                $xml.="<pubDate><![CDATA[".$page['pubDate']."]]></pubDate>";
                $xml.="<url><![CDATA[".$href."]]></url>";
                $xml.="<parent><![CDATA[".$page['parent']."]]></parent>";
                $xml.="<title><![CDATA[".$page['title']."]]></title>";
                $xml.="<menuOrder><![CDATA[".$page['menuOrder']."]]></menuOrder>";
                $xml.="<menu><![CDATA[".$page['menu']."]]></menu>";
                $xml.="<menuStatus><![CDATA[".$page['menuStatus']."]]></menuStatus>";
                $xml.="<private><![CDATA[".$page['private']."]]></private>";
                $xml.="</item>";
            }
            $xml.="</channel>";
            return $xml;
        }
        closedir($dir_handle);
    }

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
    function menu_sorting($a, $b) {
            $c1=strcmp($a->menuOrder, $b->menuOrder);
            if ($c1!=0) return $c1;
            $c2=strcmp(strtolower(($a->menu!=""?$a->menu:$a->title)), strtolower(($b->menu!=""?$b->menu:$b->title)));
            if ($c2!=0) return $c2;
            $c3=strcmp($a->slug, $b->slug);
            if ($c3!=0) return $c3;
        }
        
    function menu_master($a='') {
        if (!isset($a) || $a == '' || empty($a)) { $a = return_page_slug(); }
        $data = simplexml_load_string(ye_olde_data());
        $menu = $data->xpath('//*[menuStatus="Y"][parent=""]');
    
        usort($menu,"menu_sorting");
        if (count($menu) > 0) {
            echo '<ul class="menu">';
            foreach ($menu as $link) {
                if ("$link->slug" == "") $link->slug = "index";
                $prnt = count($data->xpath('//item[slug="'.$a.'"][parent="'.$link->slug.'"]'));
                echo '<li'.("$link->slug"==$a?' class="act"':($prnt>0?' class="act"':'')).'><a href="'.$link->url.'">'.($link->menu!=""?$link->menu:$link->title).'</a>';
                $menu = $data->xpath('//*[menuStatus="Y"][parent="'.$link->slug.'"]');
                usort($menu,"menu_sorting");
                if (count($menu) > 0) {
                    echo '<div><ul class="submenu">';
                    foreach ($menu as $link) {
                        echo '<li'.("$link->slug"==$a?' class="active"':'').'><a href="'.$link->url.'">'.($link->menu!=""?$link->menu:$link->title).'</a></li>';
                    }
                    echo '</ul></div>';
                }
                echo '</li>';
            }
            echo '</ul>';
        }
    }
    function submenu_master($a='') {
        if (!isset($a) || $a == '' || empty($a)) { $a = return_page_slug(); }
        $data = simplexml_load_string(ye_olde_data());
        $menu = $data->xpath('//*[menuStatus="Y"][parent="'.$a.'"]');

        usort($menu,"menu_sorting");
        if (count($menu) > 0) {
            echo '<ul class="menu">';
            foreach ($menu as $link) {
                if ("$link->slug" == "") $link->slug = "index";
                $prnt = count($data->xpath('//item[slug="'.$a.'"][parent="'.$link->slug.'"]'));
                echo '<li'.("$link->slug"==$a?' class="act"':($prnt>0?' class="act"':'')).'><a href="'.$link->url.'">'.($link->menu!=""?$link->menu:$link->title).'</a>';
                $menu = $data->xpath('//*[menuStatus="Y"][parent="'.$link->slug.'"]');
                usort($menu,"menu_sorting");
                if (count($menu) > 0) {
                    echo '<div><ul class="submenu">';
                    foreach ($menu as $link) {
                        echo '<li'.("$link->slug"==$a?' class="active"':'').'><a href="'.$link->url.'">'.($link->menu!=""?$link->menu:$link->title).'</a></li>';
                    }
                    echo '</ul></div>';
                }
                echo '</li>';
            }
            echo '</ul>';
        }
    }

/* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */

}

?>

now you have the function submenu_master(); which you can list all the sub pages of the current page ...
Ok but it's nos sufficient ...
I would like to list evry subpages of the parent page of the current page ...

did you understand ?
Reply
#7
Yes caviar, That is what this thread is asking.

I'm struggling to find a way of determining if the current page has a parent or not. Is this a pre-built function?

thanks
Reply
#8
Code:
if (strlen($parent)>0) {
   // This page has a parent page.
} else {
   // This page doesn’t have a parent page.
}
In your template you have access to the $parent variable. If a page has a parent its slug can be found there. If the variable is empty there is no parent-slug and, simply put, there is no parent page.
“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
#9
Sorry for the thread necromancy.

I want to know if there are sub-pages to a page. Typically these subpages would NOT be in the menu tree.

I'd like a function: get_child_slugs('page-slug')

that returns an array of child page slugs or null so I could for-each through them and build a menu (after content) listing all the child pages.

I see the code here should do this (or is close). Do I have to implement it as a plugin or can I just include this stuff in a custom functions.php?

-Rob A>
Reply
#10
RobA Wrote:Sorry for the thread necromancy.

I want to know if there are sub-pages to a page. Typically these subpages would NOT be in the menu tree.

I'd like a function: get_child_slugs('page-slug')

that returns an array of child page slugs or null so I could for-each through them and build a menu (after content) listing all the child pages.

I see the code here should do this (or is close). Do I have to implement it as a plugin or can I just include this stuff in a custom functions.php?

-Rob A>

I don;t know how correct it is to respond to myself, but I figured I'd post the code I went with. I put the following function in my functions.php file of my theme:
Code:
function return_child_data()
{
  $slug=return_page_slug();    
  $path="data/pages/";
  $data='';
  
  $count=0;
  $morderArray = array();
  if (is_dir($path))
  {
      if ($dh = opendir($path))
      {
         while (($file = readdir($dh)) !== false)
         {
             if($file!="." AND $file!=".." AND $file!=".htaccess")
                {
                    $pathtofile="$path$file";            
                    $da = @fopen($pathtofile,"r");
                    $data=getXML($pathtofile);  
                    if ($data->parent != '')
                        {
                if (strcasecmp($data->parent, $slug) == 0)
                {        
                $node=$data->children();
//print_r($node);
                $morderArray[$count]['slug'] = (string)$node->url;
                $morderArray[$count]['title'] = (string)$node->menu;
                $count++;
                 }    
                        }
            @fclose($da);
                }
         }  
         closedir($dh);
      }
  }

  return($morderArray);
}

And use this in a page template php code chunk like so:

Code:
$subpagelist = return_child_data();
foreach($subpagelist as $subpage) {
  echo $subpage['slug']; // echos the subpage slug string you can use/append as a URL
  echo $subpage['title']; // echos the subpage title string you can use a a label
}

-Rob A>
Reply
#11
Code looks pretty good to me, though I recommend on replacing
Code:
$path="data/pages/";
with
Code:
$path=GSDATAPAGESPATH;
“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
#12
Zegnåt Wrote:Code looks pretty good to me, though I recommend on replacing
Code:
$path="data/pages/";
with
Code:
$path=GSDATAPAGESPATH;

That works. Where are constants like that documented?

Thanks!

-Rob A>
Reply
#13
The bottom of http://get-simple.info/docs/plugin-creation
- Chris
Thanks for using GetSimple! - Download

Please do not email me directly for help regarding GetSimple. Please post all your questions/problems in the forum!
Reply




Users browsing this thread: 1 Guest(s)