Last active
January 31, 2018 09:45
-
-
Save doginthehat/d0c7fc1d1b5045d03af5 to your computer and use it in GitHub Desktop.
Wordpress Pages -> Menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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