Skip to content

Instantly share code, notes, and snippets.

@stetro
Created October 28, 2012 09:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stetro/3968196 to your computer and use it in GitHub Desktop.
Save stetro/3968196 to your computer and use it in GitHub Desktop.
Wordpress Subnavigation aus Navigationen (wp_nav_menu())
<ul id="subnavigation">
<?php
function my_nav_submenu_objects( $items, $args ) {
$subnav_elements = array();
// Sammle alle Subnavielemente
foreach ($items as $key => $value) {
if($value->menu_item_parent != 0){
$subnav_elements[$key]=$value;
}
}
// Vergleiche menu_item_parent mit selektiertem Element
foreach ($items as $key => $value) {
if($value->current)
{
if($value->menu_item_parent != 0)
{
$searchID = $value->menu_item_parent;
}
else
{
$searchID = $value->db_id;
}
foreach ($subnav_elements as $keyy => $valuee) {
if($valuee->menu_item_parent != $searchID)
{
unset($subnav_elements[$keyy]);
}
}
}
}
return $subnav_elements;
}
add_filter( 'wp_nav_menu_objects', 'my_nav_submenu_objects', 0, 2 );
$defaults = array(
'container' => '',
'menu' => 'Hauptnavigation',
'items_wrap' => '%3$s',
'depth' => 2
);
?>
<?php
wp_nav_menu( $defaults );
remove_filter( 'wp_nav_menu_objects', 'my_nav_submenu_objects',0,2);
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment