Artykuły na każdy temat
[PHP][WordPress] Rozwijane menu
<?php
/*
Plugin Name: Fold Page Menus for wp_nav_menu()
Plugin URI:
Description:
Version: 1.0
Dated: 18 Oct 2011
Author: CapaciousCore
Author URI: http://www.capaciouscore.pl/
*/
function fold_wp_nav_menu()
{
global $id, $wpdb;
if($id)
{
if(!is_page())
{
return wp_nav_menu(array('depth' => 1));
}
else
{
$ancestors = get_ancestors($id, 'page');
$parent = ($ancestors ? end($ancestors) : $id);
$pages = get_pages(array('post_type' => 'page'));
$childrens = get_page_children($parent, $pages);
if($childrens)
{
// Exceptions of major parents
foreach($pages as $value)
{
if(!$value -> post_parent)
{
$exceptions[] = $value -> ID;
}
}
// Exceptions of children
foreach($childrens as $value)
{
$exceptions[] = $value -> ID; // here can be a problem
}
return wp_nav_menu(array('exceptions' => $exceptions, 'walker' => new fold_wp_nav_menu()));
}
else
{
return wp_nav_menu(array('depth' => 1));
}
}
}
}
class fold_wp_nav_menu extends Walker_Nav_Menu
{
function walk($elements, $max_depth)
{
$args = array_slice(func_get_args(), 2);
$output = '';
if($max_depth < -1) //invalid parameter
return $output;
if(empty($elements)) //nothing to walk
return $output;
$id_field = $this -> db_fields['id'];
$parent_field = $this -> db_fields['parent'];
// Temporary solution
if($args[0] -> exceptions && $elements) // here id not parent id
{
foreach($elements as $key => $value)
{
if(!in_array($value -> object_id, $args[0] -> exceptions))
{
unset($elements[$key]);
}
}
}
// flat display
if(-1 == $max_depth)
{
$empty_array = array();
foreach ($elements as $e)
$this -> display_element($e, $empty_array, 1, 0, $args, $output);
return $output;
}
/*
* need to display in hierarchical order
* separate elements into two buckets: top level and children elements
* children_elements is two dimensional array, eg.
* children_elements[10][] contains all sub-elements whose parent is 10.
*/
$top_level_elements = array();
$children_elements = array();
foreach($elements as $e)
{
if(0 == $e -> $parent_field)
$top_level_elements[] = $e;
else
$children_elements[$e -> $parent_field ][] = $e;
}
/*
* when none of the elements is top level
* assume the first one must be root of the sub elements
*/
if(empty($top_level_elements))
{
$first = array_slice($elements, 0, 1);
$root = $first[0];
$top_level_elements = array();
$children_elements = array();
foreach($elements as $e)
{
if($root -> $parent_field == $e -> $parent_field)
$top_level_elements[] = $e;
else
$children_elements[$e -> $parent_field ][] = $e;
}
}
foreach($top_level_elements as $e)
$this -> display_element($e, $children_elements, $max_depth, 0, $args, $output);
/*
* if we are displaying all levels, and remaining children_elements is not empty,
* then we got orphans, which should be displayed regardless
*/
if(($max_depth == 0) && count($children_elements) > 0)
{
$empty_array = array();
foreach($children_elements as $orphans)
foreach($orphans as $op)
$this->display_element($op, $empty_array, 1, 0, $args, $output);
}
return $output;
}
}
?>
A potem aby zobaczyć menu wystarczy odpalić:
<?php fold_wp_nav_menu(); ?>
Banalne, prawda? Spakowany plik można ściągnąć stąd.
Komentarze
Dodaj komentarz
CapaciousCore