Support » Fixing WordPress » Get children menu items of a custom menu item

  • I am using a snippet to display the children pages in a sidebar of a page. Here is the snippet I am using.

    <?php
    
    	if ($post->post_parent)	{
    		$ancestors=get_post_ancestors($post->ID);
    		$root = count($ancestors)-1;
    		$parent = $ancestors[$root];
    	} else {
    		$parent = $post->ID;
    	}
    
    	print '<h2><a href="'.get_permalink($parent).'">'.get_the_title($parent).'</a></h2>';
    
    	$children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0&depth=1");
    
    	if ($children) : ?>
    	<ul>
    	<?php echo $children; ?>
    	</ul>
    
    <?php endif; ?>

    This snippet works great for pages that are actually part of the WordPress. But when using it when the top parent is a custom menu item (Place Holder) it only grabs the top parent title and not the menu items under it.

    I’m extremely familiar with WordPress and I am hoping I am not having a blonde moment here ( No offensive attended).

Viewing 2 replies - 1 through 2 (of 2 total)
  • I don’t think that it’s possible. Menu items have their own custom post type (Navigation Menu). You can try to get the right post (Navigation Menu) using wp_query and find its child. But it’s much more complex.

    Or you make the same hierarchy on the pages and in your navigation, et voila !

    You may be right.. I was able to do something like this and get just the menu items

    <?php
    /**
     * The follow look into the menu items and displays them by menu items and not menu's or pages
     */
    $menuItems = wp_get_nav_menu_items('main-menu');
    foreach($menuItems as $menuItem) {
    	if($menuItem->post_title == 'Media Center')
    		print_r($menuItem);
    }
    ?>

    Then I could say if the title is “Media Center” Show all information about it but this is not showing a dump as expected. I know the snippet in my first post would not work but just wanted to see if anyone had a more better idea for how to tackle this. However I think the snippet in this post has gotten me closer to the end result I am looking for.

    Thanks for posting

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get children menu items of a custom menu item’ is closed to new replies.