Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manu/Navigation helper functions
#1
Hi-

I am using a few custom functions in my site that I thought others might find handy.

I use child pages like so:
Gallery
-page1
-page2
-etc

And want gallery to be in my main menu, but all the child pages are NOT. I wanted to generate the gallery page dynamically, based on the child pages (add a new child page and the gallery page just updates) and I wanted navigation in the child pages (perv/next).

You can see the working code in my gallery page:


http://www.cartocopia.com/portfolio/


These calls parse all the pages' files so can be slow. I use my simplecache plugin, so after the first cache it is quite efficient:

Code:
<?php
/**
* custom function to return all the children of the page the function
* is called on.  Returns an array consisting of ('slug', 'title', 'order')
* sorted by order (which comes from the menuorder field of pages.  As
* a tip, you need to check "show page in menu" to set the menuorder value
* and then the display in menu can be unset so the childpage doesn't
* show up in the menu.
* ----
* call like:
* $childpages = return_child_data;
*
* optionally provide a different page slug as a parameter to get the children of a
* different slug.  This method is used in the prev and next functions:
* $childpages = return_child_data('differentpageslug');
* ----
* access data like:
* foreach($childpages as $page) {
*   echo "Slug: " . $page['slug'];
*   echo "Title: " . $page['title'];
*   echo "Order: " . $page['order'];
*  }
*/
function return_child_data($slug='') {
  if ($slug == '') {
    $slug = return_page_slug();
  }
  $path=GSDATAPAGESPATH;
  $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) && ($data->private !='Y')) {        
                $node=$data->children();
                $morderArray[$count]['slug'] = (string)$node->url;
                $morderArray[$count]['title'] = (string)$node->title;
                $morderArray[$count]['order'] = (string)$node->menuOrder;
                $count++;
                 }    
                        }
            @fclose($da);
                }
         }  
         closedir($dh);
      }
  }

  $morderArray = subval_sort($morderArray,'order');
  
  return($morderArray);
}
/** end of function return_child_data() ---------------------*/


/**
* custom function to return the slug of the next page in a group of child pages
* this will wrap around so the "next" page from the last page is the "first" page
* The order is based on the menuorder of the pages.
* ----
* example usage in a template:
* <a href="<?php get_parent(TRUE); echo '/'; return_next_slug(); ?>">Next Page</a>
*/
function return_next_slug() {
  $slug = return_page_slug();
  $peers = return_child_data(get_parent(FALSE));

  $size = count($peers);
    foreach($peers as $count => $pagedata) {
    if ($pagedata['slug'] == $slug) {
      return($peers[($count + 1) % $size]['slug']);
    }
  }
  return(FALSE);
}
/** end of function return_next_slug() ---------------------*/

/**
* custom function to return the slug of the previous page in a group of child pages
* this will wrap around so the "previous" page from the first page is the "last" page
* The order is based on the menuorder of the pages.
* ----
* example usage in a template:
* <a href="<?php get_parent(TRUE); echo '/'; return_prev_slug(); ?>">Previous Page</a>
*/
function return_prev_slug() {
  $slug = return_page_slug();
  $peers = return_child_data(get_parent(FALSE));
  
  $size = count($peers);
  foreach($peers as $count => $pagedata) {
    if ($pagedata['slug'] == $slug) {
      return($peers[($count + $size - 1) % $size]['slug']);
    }
  }
  return(FALSE);
}
/** end of function return_prev_slug() ---------------------*/

?>

Hopefully others might find this useful.

(I' really like a standard way of caching all of the menu data as an XML file to get into the core rather than various plugins all doing variations of the same things, such as the i18n, the multilevel menu plugin, the pagecache plugin, etc.)

-Rob A>
Reply


Messages In This Thread
Manu/Navigation helper functions - by RobA - 2011-04-30, 05:08:18
Manu/Navigation helper functions - by mvlcek - 2011-04-30, 21:51:32
Manu/Navigation helper functions - by mvlcek - 2011-04-30, 21:59:26
Manu/Navigation helper functions - by RobA - 2011-05-01, 02:49:41
Manu/Navigation helper functions - by mvlcek - 2011-05-03, 00:11:53
Manu/Navigation helper functions - by RobA - 2011-05-03, 02:05:16
Manu/Navigation helper functions - by n00dles101 - 2011-05-03, 03:21:09
Manu/Navigation helper functions - by RobA - 2011-05-04, 00:08:06



Users browsing this thread: 1 Guest(s)