• I’m struggling to create a vertical submenu. I found a lot of solutions here, but none seems to do exactly what I want.

    I’ve got a page structure like this:

    Page 1
    – Page 1.1
    – Page 1.2
    —> Page 1.2.1
    —> Page 1.2.2
    – Page 1.3
    —> Page 1.3.1
    —> Page 1.3.2
    – Page 1.4
    Page 2
    Page 3
    etc…

    The Pages 1 to 4 (the top level) are in the main navigation.

    But now I want to have a submenu on Page 1 that lists
    – Page 1.1
    – Page 1.2
    – Page 1.3
    – Page 1.4

    And if go to page 1.2, it should display
    – Page 1.1
    – Page 1.2
    —> Page 1.2.1
    —> Page 1.2.2
    – Page 1.3
    – Page 1.4

    I want to to display only the actual 2nd level, and also the actual page itself

    What makes things a little more difficult: I need to find a solution using get_pages(), as I use a Bootstrap 3 Theme which doesn’t support multi-level menus. And when using wp_list_pages(), I can’t edit the CSS enough for my taste.

    I know for get_pages() there is parent, include, exclude, exclude_tree and child_of to play with. But no combination seems to get me where I want to go.

    Any help would be appreciated.

Viewing 1 replies (of 1 total)
  • Thread Starter Holly73

    (@holly73)

    I’m back with a solution.

    What I came up with might not be the most elegant, but it works:

    <ul class="nav nav-pills nav-stacked">
    
    <?php
    $ActualPage = get_post();
    $TopPage = get_post_top_ancestor_id();
    
    // Always display Top Page:
    $TopLevel = get_pages('include='.get_post_top_ancestor_id());
    foreach ( $TopLevel as $page ) {
       $TopItem = '<li class="toplevel"><a href="' . get_page_link( $page->ID ) . '">';
       $TopItem .= $page->post_title;
       $TopItem .= '</a></li>';
       echo $TopItem;
    }
    
    // Level 1
    $Level1 = get_pages('child_of='.$TopPage.'&sort_column=menu_order&post_status=publish');
    foreach ( $Level1 as $page1 ) {
       // exclude Level 2 from the list:
       if ($page1->post_parent == $TopPage) {
          // Highlight Active
          if ($page1->ID == $ActualPage->ID) {
          $L1Item = '<li class="active"><a href="' . get_page_link( $page1->ID ) . '">';
          } else {
          $L1Item = '<li><a href="' . get_page_link( $page1->ID ) . '">';
       }
       $L1Item .= $page1->post_title;
       $L1Item .= '</a></li>';
       echo $L1Item;
       // Check if page is actual page and if it has children or if we are on a Level 2 page
       if ((($page1->ID == $ActualPage->ID)&&(has_children($ActualPage->ID)))||($ActualPage->post_parent == $page1->ID)) {
       // Level 2
       $Level2 = get_pages('child_of='.$page1->ID.'&sort_column=menu_order&post_status=publish');
       foreach ( $Level2 as $page2 ) {
          // Highlight Active
          if ($page2->ID == $ActualPage->ID) {
             $L2Item = '<li class="second active"><a href="' . get_page_link( $page2->ID ) . '">';
             } else {
             $L2Item = '<li class="second"><a href="' . get_page_link( $page2->ID ) . '">';
          }
          $L2Item .= '<i class="glyphicon glyphicon-hand-right"></i>  ?'.$page2->post_title;
          $L2Item .= '</a></li>';
          echo $L2Item;
       }
    }
    }
    }
    
    ?>
    
    </ul>
Viewing 1 replies (of 1 total)
  • The topic ‘Menu to display child and grandchild pages, but not always’ is closed to new replies.