WordPress.org

Ready to get started?Download WordPress

Forums

custom menus (2 posts)

  1. rogerwalsh
    Member
    Posted 4 years ago #

    Hi,

    Looking for a bit of advice. I'm looking to create a menu stucture in columns, up to five...something functioning like the nav system on:

    http://www.whynotassociates.com

    so clicking a top level in column one shows it's children in column two, clicking a child in column two shows it's children in column 3.

    have been reading the codex page on wp_list_pages( $args ); but just can't seem to get my head around it.

    Any suggestions or guidance most welcome!

    Thanks in advance

  2. ambrosite
    Member
    Posted 4 years ago #

    Each one of those menus must be created with a separate call to wp_list_pages. It's easy enough to create a child menu using the 'child_of' and 'depth' parameters. The tricky part is generating the menu hierarchy. One way to do this is to climb the page ladder from the current page up to the top level, building an array of page IDs as you go, and then loop through that array in reverse order to generate the menus.

    The following code should do the trick. This is meant to be used outside the loop -- in the header, or at the very top of the page template.

    <?php
    // Get the page ladder for the current page
    global $wp_query;
    $my_page = get_page($wp_query->get_queried_object_id());
    $page_ladder = array($my_page->ID);
    while ( $my_page->post_parent ) {
        $my_page = get_page($my_page->post_parent);
        $page_ladder[] = $my_page->ID;
    }
    
    // Top level menu
    wp_list_pages('title_li=MySite.com&depth=1');
    
    // Child menus (if any)
    $page_ladder = array_reverse($page_ladder);
    foreach( $page_ladder as &$my_page ) {
        wp_list_pages('title_li=' . get_the_title( prev($page_ladder) ) . '&depth=1&child_of='.$my_page);
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic