WordPress: get current menu item children

For one of my websites I would like to show a list of the current menu item’s (direct) children when the page is empty. I have a piece of code that is partly working as long as I use posts or pages in my menu. The second I start adding categories or custom taxonomies, they are not showing up anymore in the list. Looking at my code, it is obvious it doesn’t show, since categories are taxonomies and no posts, but I have no idea how to approach this issue.

Any ideas as to how I can get the current menu item’s direct children? I only need the level below the current item.

add_filter('the_content', 'the_children');
function the_children($content)
    if($content == ''){
        $menu_items = get_posts(array(
            'post_parent'   =>  get_the_ID(),
            'post_type'     =>  'nav_menu_item',
            'orderby'       =>  'menu_order',
            'order'         =>  'ASC'

        if(count($menu_items)) {
            $result = '<ul class="children">';
                foreach($menu_items as $menu_item){
                    $menu_item = wp_setup_nav_menu_item($menu_item);

                    $result .= '<li>';
                        $result .= sprintf('<a href="%s">%s</a>', $menu_item->url, $menu_item->title);
                    $result .= '</li>';
            $result .= '</ul>';
        return $result;
    else return $content;

Read more here: WordPress: get current menu item children

Leave a Reply

Your email address will not be published. Required fields are marked *