WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic Nav Menu - from functions.php with Automatically add new top-level pages (1 post)

  1. toto
    Member
    Posted 2 years ago #

    Hi friends,

    i create default dynamic (hard coded) menu in functions.php with this code:

    // Create Default Primary Menu with pages
      $name = 'Primary Menu';
      $menu_id = wp_create_nav_menu($name);
      $menu = get_term_by( 'name', $name, 'nav_menu' );
      wp_update_nav_menu_item($menu->term_id, 0, array(
          'menu-item-title' =>  __('Home'),
          'menu-item-classes' => 'home',
          'menu-item-url' => home_url( '/' ),
          'menu-item-status' => 'publish'));
      // you add as many items ass you need with wp_update_nav_menu_item()
    
      //then you set the wanted theme  location
      $locations = get_theme_mod('nav_menu_locations');
      $locations['primary-menu'] = $menu->term_id;
      set_theme_mod( 'nav_menu_locations', $locations );

    So its create new primary menu with one page by default when theme activated.

    Is there a possibility to set option for this menu "Automatically add new top-level pages" to be turned on. And if user add new page to be placed automatically in menu?

    Maybe I need to use "update_option( 'nav_menu_options');"?

Topic Closed

This topic has been closed to new replies.

About this Topic