WordPress.org

Ready to get started?Download WordPress

Forums

Get and display parent pages of a menu. (6 posts)

  1. marcoteu
    Member
    Posted 3 years ago #

    I have a horizontal dropdown menu on top, with 4 custom links as parent menu items and some child pages underneath one of each.

    In the side-bar I want to show the user (because the main horizontal menu is a dropdown menu) a second navigation list with on top the title of the current (one of the four) main parent menu items and underneath all the child pages of that specific parent menu item.

    With wp_list_pages() it was pretty easy but I have no clue of fixing this thing with the new wp_nav_menu().

    I used this link to get me started about making a custom walker.
    http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output

    But I have no idea to move further.

    Anybody some ideas or experience with this issue?

    Thanks!
    Marco

  2. marcoteu
    Member
    Posted 3 years ago #

    With wp_list_pages I use a code like this underneath. Can something similair been made with wp_nav_menu()?

    <?php
    $parent_title = get_the_title($post->post_parent);
    
    							$output = wp_list_pages('echo=0&depth=1&title_li=Top Level Pages&exclude=13,2' );
    
    							if (is_page( )) {
      							$page = $post->ID;
      							if ($post->post_parent) {
        							$page = $post->post_parent;
      							}
      								$children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
      								if ($children) {
       								 $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li='.$parent_title.' ');
      							}
    								}
    								echo $output;
    		 }
    ?>
  3. vaseemansari
    Member
    Posted 3 years ago #

    Is anyone out there proposing the answer of same question written above. I am looking for the same

  4. marcoteu
    Member
    Posted 3 years ago #

    I hope this question gets some replies. It would be great so see how to deal with wp_nav_menu() in this situation.

  5. cammanderson
    Member
    Posted 2 years ago #

    Hi Guys,

    Maybe unrelated, but I had to try and locate the parent, then maybe someone else can figure out the listing bit. I just needed to link back to the parent if it was a page.

    // Traverse and locate the parent structure on the main menu
            // 2n = slow search
            $menuItems = wp_get_nav_menu_items('main-menu');
            foreach($menuItems as $menuItem) {
                if($menuItem->object_id == $post->ID && $menuItem->object == $post->post_type) {
                    $parentMenuId = $menuItem->menu_item_parent;
                    break;
                }
            }
            foreach($menuItems as $menuItem) {
                if($menuItem->ID == $parentMenuId && $menuItem->object == 'page') {
                    $pageId = $menuItem->object_id;
                    $menuId = $menuItem->ID;
                    break;
                }
            }
    
            // Link back up to the page
            $content = '<a href="' . get_page_link($pageId) . '">' . get_the_title($pageId) . '</a>';

    Not the nicest way, but it was just the quickest way without finding a nice API method. Theoretically someone else could contribute displaying the children thing once you have the $id. :-) cheers

  6. marcoteu
    Member
    Posted 2 years ago #

    Thanks for your reply Cammanderson!
    Your code looks promising. I'll look into it.

    Grts!
    Marco

Topic Closed

This topic has been closed to new replies.

About this Topic