| 1 |
<?php $run_once = get_option('menu_check'); |
| 2 |
if (!$run_once){ |
| 3 |
//give your menu a name |
| 4 |
$name = 'primary'; |
| 5 |
//create the menu |
| 6 |
$menu_id = wp_create_nav_menu($name); |
| 7 |
//then get the menu object by its name |
| 8 |
$menu = get_term_by( 'name', $name, 'nav_menu' ); |
| 9 |
|
| 10 |
//then add the actuall link/ menu item and you do this for each item you want to add |
| 11 |
wp_update_nav_menu_item($menu->term_id, 0, array( |
| 12 |
'menu-item-title' => __('Home'), |
| 13 |
'menu-item-url' => '#totop', |
| 14 |
'menu-item-status' => 'publish', |
| 15 |
'menu-item-position' => 1, |
| 16 |
)); |
| 17 |
|
| 18 |
wp_update_nav_menu_item($menu->term_id, 0, array( |
| 19 |
'menu-item-title' => __('Service'), |
| 20 |
'menu-item-url' => '#services', |
| 21 |
'menu-item-status' => 'publish', |
| 22 |
'menu-item-position' => 2, |
| 23 |
)); |
| 24 |
|
| 25 |
wp_update_nav_menu_item($menu->term_id, 0, array( |
| 26 |
'menu-item-title' => __('About'), |
| 27 |
'menu-item-url' => '#about', |
| 28 |
'menu-item-status' => 'publish', |
| 29 |
'menu-item-position' => 3, |
| 30 |
)); |
| 31 |
|
| 32 |
wp_update_nav_menu_item($menu->term_id, 0, array( |
| 33 |
'menu-item-title' => __('Team'), |
| 34 |
'menu-item-url' => '#team', |
| 35 |
'menu-item-status' => 'publish', |
| 36 |
'menu-item-position' => 4, |
| 37 |
)); |
| 38 |
|
| 39 |
wp_update_nav_menu_item($menu->term_id, 0, array( |
| 40 |
'menu-item-title' => __('Blog'), |
| 41 |
'menu-item-url' => '#blog', |
| 42 |
'menu-item-status' => 'publish', |
| 43 |
'menu-item-position' => 5, |
| 44 |
)); |
| 45 |
|
| 46 |
wp_update_nav_menu_item($menu->term_id, 0, array( |
| 47 |
'menu-item-title' => __('Contact'), |
| 48 |
'menu-item-url' => '#contact', |
| 49 |
'menu-item-status' => 'publish', |
| 50 |
'menu-item-position' => 6, |
| 51 |
)); |
| 52 |
|
| 53 |
wp_update_nav_menu_item($menu->term_id, 0, array( |
| 54 |
'menu-item-title' => __('Callout'), |
| 55 |
'menu-item-url' => '#call-to-action', |
| 56 |
'menu-item-status' => 'publish', |
| 57 |
'menu-item-position' => 7, |
| 58 |
)); |
| 59 |
|
| 60 |
|
| 61 |
//then you set the wanted theme location |
| 62 |
$locations = get_theme_mod('nav_menu_locations'); |
| 63 |
$locations['main-menu'] = $menu->term_id; |
| 64 |
set_theme_mod( 'nav_menu_locations', $locations ); |
| 65 |
|
| 66 |
// then update the menu_check option to make sure this code only runs once |
| 67 |
update_option('menu_check', true); |
| 68 |
} ?> |