0

The official Twenty Thirteen theme defines just a single menu in the /wp-content/themes/twentythirteen/header.php file:

<?php wp_nav_menu( array( 
    'theme_location' => 'primary', 
    'menu_class' => 'nav-menu', 
    'menu_id' => 'primary-menu' ) ); ?>

In the child theme of my website I am trying to populate that menu programmatically in the /wp-content/themes/twentythirteen-child/functions.php file, because:

  1. It makes reinstalling the website easier (less manual steps).
  2. The last menu item should point to /player-ID link.

So I have tried to add the code:

function my_register_menus() {
    register_nav_menus( array(
            'primary-menu' => __( 'Primary Menu' ),
         )
    );
}

add_action( 'init', 'my_register_menus' );

but that does not change anything in the appearance of the website.

Also I have (re)read Navigation Menus in Theme Handbook (incl. API links) and still don't understand, how to pass an array of links to the menu.

I probably should call wp_nav_menu() with some array of objects, but can not find the desciption of such objects.

At the end I would like to add 4 links to the menu:

  1. "Play game" - /
  2. "Android" - /android
  3. "iOS" - /ios
  4. "Your profile" - /player-ID (will take ID from wp_get_current_user())
2

Here you go. Since you already have the menu-id all you need is:

$menu_item_defaults = array(
                    'menu-item-db-id' => $menu_item_db_id,
                    'menu-item-object-id' => 0,
                    'menu-item-object' => '',
                    'menu-item-parent-id' => 0,
                    'menu-item-position' => 1,
                    'menu-item-title' => 'Play Game',
                    'menu-item-url' => '',//not sure is a blank is ok here
                    'menu-item-description' => '',
                    'menu-item-attr-title' => '',
                    'menu-item-target' => '',
                    'menu-item-classes' => '',
                    'menu-item-xfn' => '',
                    'menu-item-status' => 'publish',
                );

wp_update_nav_menu_item('primary-menu', 0, $menu_item_defaults);

However, it you're trying to speed deployment of a site with known content try it this way. You can create the page and add it to the menu all on the fly.

In my case I'm creating a custom menu first but you can use this to get the id of any menu.

$menu_exists = wp_get_nav_menu_object( 'Public Menu' );
if ( $menu_exists === false ) {
    $var_pub_menu_id = wp_create_nav_menu( 'Public Menu' );
} else {
    $var_pub_menu_id = $menu_exists->term_id;
}

Then I create my page and add it to the menu:

$default_fgpsn_page = array(
    'post_title'    => $fgpsn_default_pages[$j],//any page title. I'm working in a loop here
    'post_status'   => 'publish',
    'post_type'   => 'page',
    'post_content'   => 'Property Browse Short Code',//this can even be a shortcode
    'post_author'   => 1
    );

   $default_fgpsn_page_id = wp_insert_post( $default_fgpsn_page );

            $menu_item_defaults = array(
                //'menu-item-db-id' => $menu_item_db_id,
                //'menu-item-object-id' => 0,
                //'menu-item-object' => '',
                'menu-item-parent-id' => 0,
                'menu-item-position' => 1,
                'menu-item-title' => $fgpsn_default_pages[$j],
                'menu-item-url' => get_page_link( $default_fgpsn_page_id ),
                'menu-item-description' => 'public properties listing',
                'menu-item-attr-title' => '',
                'menu-item-target' => '',
                'menu-item-classes' => '',
                'menu-item-xfn' => '',
                'menu-item-status' => 'publish',
            );

            wp_update_nav_menu_item($var_pub_menu_id, 0, $menu_item_defaults);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.