Skip to content

Instantly share code, notes, and snippets.

@doginthehat
Last active January 31, 2018 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save doginthehat/d0c7fc1d1b5045d03af5 to your computer and use it in GitHub Desktop.
Save doginthehat/d0c7fc1d1b5045d03af5 to your computer and use it in GitHub Desktop.
Wordpress Pages -> Menu
/*
Generate a hierachical menu based on the page structure of your wordpress website.
Useful if your late in the game in generating a menu, or updating an old website.
If you don't understand what it does, don't use it.
*/
$pages = get_pages(array('sort_column'=>'menu_order,post_title'));
$map = array();
$menu_id = wp_create_nav_menu('Main Menu');
foreach($pages as $page) {
$data = array('menu-item-title' => $page->post_title,
'menu-item-object' => 'page',
'menu-item-object-id' => $page->ID,
'menu-item-type' => 'post_type',
'menu-item-status' => 'publish');
if ($page->post_parent != 0) {
$parent_id = $map[$page->post_parent];
$data['menu-item-parent-id'] = $parent_id;
}
$map[$page->ID] = wp_update_nav_menu_item($menu_id, 0, $data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment