buckdanny
buckdanny

Reputation: 357

Wordpress: Add item to Submenu

I need to add a items via a function to a specific Submenu, how can i do this? I did a quick sketch to show what I need, thanks for any help!

Sketch:

add_filter('wp_nav_menu_items','specialprojekte_in_projekte_submenu', 10, 2);
function specialprojekte_in_projekte_submenu( $items, $args ) { 
    if( ???SUBMENU == PROJECTS ???)         
        $items .="<li> this is a special project at the end of projects submenu </li>";         
    }
    return $items;
} 

Upvotes: 0

Views: 2521

Answers (1)

pgk
pgk

Reputation: 1477

If you want to use filter to modify the menu, the filter in witch must to hook is wp_get_nav_menu_items

add_filter('wp_nav_menu_items','specialprojekte_in_projekte_submenu', 10, 2);

In this hook first parameter gives you all menu elements as array. Each menu element is WP_Post_Object with properties:

WP_Post Object
(
    [ID] => 6
    [post_author] => "1"
    [post_date] => "2015-12-06 19:07:48"
    [post_date_gmt] => "2015-12-06 17:07:48"
    [post_content] => ""
    [post_title] => "Post Title"
    [post_excerpt] => ""
    [post_status] => "publish"
    [comment_status] => "closed"
    [ping_status] => "closed"
    [post_password] => ""
    [post_name] => "221"
    [to_ping] => ""
    [pinged] => ""
    [post_modified] => "2016-01-13 14:05:51"
    [post_modified_gmt] => "2016-01-13 12:05:51"
    [post_content_filtered] => ""
    [post_parent] => "220"
    [guid] => "http://yoursite.dev/?p=6"
    [menu_order] => 1
    [post_type] => "nav_menu_item"
    [post_mime_type] => ""
    [comment_count] => "0"
    [filter] => raw
    [db_id] => 6
    [menu_item_parent] => "5"
    [object_id] => "6"
    [object] => "custom"
    [type] => "custom"
    [type_label] => "Menu Label"
    [title] => "First Submenu Item"
    [url] => "http://yoursite.dev/"
    [target] => ""
    [attr_title] => ""
    [description] => ""
    [classes] => Array
    [xfn] => ""
)

So you can use all of these properties to select menu or submenu on which you want to append new element. Notice that if menu has "menu_item_parent" <> 0 then this is submenu.

Then you must create your submenu object:

function specialprojekte_in_projekte_submenu( $items, $menu ) {
    $menu_cnt = count( $items ) + 1;

    //find parent menu item, in witch we wont to append our new menu
    $parent_id = 0;
    foreach ( $items as $item ) {
        //one possible argument to find what you need
        if ( $item->menu_item_parent === "201" ) {
            $parent_id = $item->ID;
            break;
        }
    }

    $items[] = (object) array(
        'ID'                => $menu_cnt + 100000, // Some big ID that WP can not use 
        'title'             => 'My new submenu',
        'url'               => '#',
        'menu_item_parent'  => $parent_id,
        'menu_order'        => $menu_cnt,
        // These are not necessary, but PHP throws warning if they dont present
        'type'              => '',
        'object'            => '',
        'object_id'         => '',
        'db_id'             => '',
        'classes'           => '',
        'post_title'        => '',
    );
    return $items;
}

And of course you can use Walker_Nav_Menu class Codex

Upvotes: 1

Related Questions