Posts: 12
Threads: 5
Joined: Feb 2010
2010-02-18, 12:44:19
(This post was last modified: 2010-02-18, 12:44:53 by pagesbytom.)
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.
Posts: 972
Threads: 27
Joined: Aug 2009
2010-02-18, 23:42:37
(This post was last modified: 2010-02-18, 23:43:13 by vsky.)
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.
Posts: 12
Threads: 5
Joined: Feb 2010
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..
Posts: 23
Threads: 5
Joined: Apr 2010
that exactly that I want to do ...
Please Help
Posts: 12
Threads: 5
Joined: Feb 2010
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.
Posts: 23
Threads: 5
Joined: Apr 2010
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 ?
Posts: 12
Threads: 5
Joined: Feb 2010
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
Posts: 972
Threads: 27
Joined: Aug 2009
2010-04-27, 17:02:13
(This post was last modified: 2010-04-27, 17:03:10 by vsky.)
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.
Posts: 290
Threads: 26
Joined: Oct 2010
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>
Posts: 290
Threads: 26
Joined: Oct 2010
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>
Posts: 972
Threads: 27
Joined: Aug 2009
Code looks pretty good to me, though I recommend on replacing
Code: $path="data/pages/";
with
Code: $path=GSDATAPAGESPATH;
Posts: 290
Threads: 26
Joined: Oct 2010
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>
Posts: 1,848
Threads: 86
Joined: Aug 2009
- 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!
|