Get child menu from current page – wordpress

Posted: October 16, 2014 in web
...
add_shortcode('menu-child', array($this, 'sc_menu_child'));
...
function sc_menu_child() {
        global $post;
        
        $menu = wp_get_nav_menu_items('main'); // all menu items array
        $menuID = 0; // we need to find $menuID that corresponds to the post ID
        $parent = $post->post_parent != 0 ? $post->post_parent : $post->ID;
        foreach ($menu as $item) {
            if ($item->object_id == $parent)
                $menuID = $item->ID;
        }        
        $submenu = array();  // all menu items under $menuID

        foreach ($menu as $item) {
            if ($item->menu_item_parent == $menuID)
                $submenu[] = $item;
        }
        $str = '';
        if ($submenu) { 
            $str .= '<ul class="ul-horizontal">';
            foreach ($submenu as $el) {
                $str .= '<li><a href="' . $el->url . '">' . $el->title . '</a>';
                $str .= '</li>';
            }
            $str .= '</ul>';
        }
        return $str;
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s