WordPress.org

Ready to get started?Download WordPress

Forums

Dynamically nav menus with wp_nav_menu ? (7 posts)

  1. maorb
    Member
    Posted 3 years ago #

    Hi,

    I try to solve an issue with navigation.
    Previously, if I had for example a page with subpages, or category with subcategories, I could just use wp_list_pages or wp_list_categories with some parameters to dynamically display sub pages/cats for every one of the pages/posts viewed.

    I want to achieve the following dynamic situation:
    There is a nav menu that should be displayed dynamically. I want to use the new wp_nav_menu function.
    Each parent page has some children/sub pages to be displayed in the nav menu, and also a different category archive link that should be displayed in that same menu for every different parent page that viewed.

    Obviously, there is no point to manually create each menu and menu for each parent page.

    How could that be dynamically accomplished with the new wp_nav_menu function?

    Thanks,
    Maor

  2. maorb
    Member
    Posted 3 years ago #

    Any ideas?

    Thanks

  3. maorb
    Member
    Posted 3 years ago #

    Any ideas...?
    I'd be happy for your thoughts about this..

  4. johnnybebopcool
    Member
    Posted 3 years ago #

    I would like to do something very similar also. One workaround I found was to create a new menu for each page you want, but that shouldn't have to be done as it is not dynamic.

    What I would like to do is show only a branch or some subset of the primary navigation set up with wp_nav_menu. So on a sub page it would dynamically create a nav menu on the side that shows only a part of the primary navigation - specifically all of the child pages to the top level parent page.

    Has anyone figured out how to do something like this? Is it even possible to do with wp_nav_menu?

  5. maorba
    Member
    Posted 3 years ago #

    After searching and reading some threads about it, I came to a conclusion that it is not possible with the new nav function.
    If you want it dynamically produced, then keep using the old and good wp_list_pages or wp_list_categories.
    This new function is not mentioned to deal with such dynamics, just to let user manually create the menu.

    If someone finds or sees something I didn't, I'd be happy to know...

  6. serdominik
    Member
    Posted 3 years ago #

    I would also concerned that this problem seems a little 'present everywhere. They should put a button to the pages if you do see all the Auto pages, or exclude some or put them automatically

  7. willsko87
    Member
    Posted 3 years ago #

    By this do you mean you want it to automatically create wp_nav_menu's for each top level page, so you can use them as sub menu's for that page?

    I created a loop that goes through all the top level pages and automatically creates a sub menu in the new wp menu editor.

    I put the following in my functions.php

    <?php
    $mypages = get_pages('parent=0');
    	foreach($mypages as $page)
    	{
    $slug = $page->post_name;
    $title = $page->post_title." Sub Nav";
    register_nav_menus(array($slug => __( $title )));
    
    	}
    ?>

    and then put this in my sidebar.php (or wherever you want the sub-menus displayed).

    <?php
    
    $mypages = get_pages('parent=0');
    $count = 0;
    	foreach($mypages as $page)
    	{
    $content = $page->post_name;
    $title = $page->post_title." Sub Nav";
    
    if(is_page($content) || is_child($content)) {
    wp_nav_menu( array( 'container_class' => 'sidebar-subnav', 'theme_location' => $content ) );
    } 
    
    	}
    ?>

    I also used the is_child plugin to make use of the is_child function so that the sub menu displays on the child pages too.

    I hope this is what you meant, if not it might come in handy for others.

Topic Closed

This topic has been closed to new replies.

About this Topic