1

I have utilised code from this posting: Display grandchildren on child and grandchild pages using wp_list_pages to create my menu here: http://new.leicesterymca.co.uk/youth-community/our-work/

As you can see, I have the top level menu showing near the logo, the second level menu showing in side the blue strip and also the 3rd (and 4th) level menus showing in a column the left hand side of the page.

The trouble I'm having is,

  1. I have no idea where the top Bullet point 'Pages' is coming from and would like to remove this
  2. I would like this menu to display as per the order inside the Wordpress menu and not alphabetically.
  3. Currently when you hover on these menus, every level pops out, I would like it so they slide out one by one when hovering, not all levels at once...

So here are the bits of code I have - I have this inside the functions.php file:

// FUNCTION FOR SETTING UP SUB MENU PAGES

// add hook
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 );

// filter_hook function to react on sub_menu flag
function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) {
  if ( isset( $args->sub_menu ) ) {
    $root_id = 0;

    // find the current menu item
    foreach ( $sorted_menu_items as $menu_item ) {
      if ( $menu_item->current ) {
        // set the root id based on whether the current menu item has a parent or not
        $root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID;
        break;
      }
    }

    // find the top level parent
    if ( ! isset( $args->direct_parent ) ) {
      $prev_root_id = $root_id;
      while ( $prev_root_id != 0 ) {
        foreach ( $sorted_menu_items as $menu_item ) {
          if ( $menu_item->ID == $prev_root_id ) {
            $prev_root_id = $menu_item->menu_item_parent;
            // don't set the root_id to 0 if we've reached the top of the menu
            if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent;
            break;
          } 
        }
      }
    }

    $menu_item_parents = array();
    foreach ( $sorted_menu_items as $key => $item ) {
      // init menu_item_parents
      if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID;

      if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) {
        // part of sub-tree: keep!
        $menu_item_parents[] = $item->ID;
      } else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) {
        // not part of sub-tree: away with it!
        unset( $sorted_menu_items[$key] );
      }
    }

    return $sorted_menu_items;
  } else {
    return $sorted_menu_items;
  }
}

// FUNCTION FOR SETTING UP SUB-SUB MENU PAGES

/** Get Top Most Page ID and Return it **/
function get_ancestor(){
    global $post;
    $id = 0;

    if(is_object($post)){
        $id = $post->ID;

        if($post->post_parent){
            $ancestors = get_post_ancestors($post->ID);
            $root = count($ancestors)-1;
            $id = $ancestors[$root];
        }
    } 
    else if(is_singular('post') || is_archive() || (is_home() && !is_front_page())){
        $id = get_option('page_for_posts');
    }

    return $id;
}

Also, here's the code in my template page I'm using to display the 3rd level menu only - as its just this 3rd level I have a problem with:

<?php $id = get_ancestor(); // Hold Top Most Page ID ?>

        <?php if($post->post_parent != 0) : /** IF we're NOT on a Top Level Page **/ ?>
            <ul>
                <?php 
                    $parentID = ($post->post_parent == $id) ? $post->ID : $post->post_parent;
                    wp_list_pages("child_of=$parentID&depth=2"); 
                ?>
            </ul>
        <?php endif; ?>
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.