-
-
Save wasipo/9dc85a9c8559afe57be106f857459fba to your computer and use it in GitHub Desktop.
WordPressのナビゲーションを親子関係を持った配列で取得するスクリプト
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
function get_navigation_data($navigation_name) { | |
$locations = get_nav_menu_locations(); | |
$menu = wp_get_nav_menu_object($locations[$navigation_name]); | |
$menu_items = wp_get_nav_menu_items($menu->term_id); | |
$result = []; | |
foreach($menu_items as $key => $item) { | |
if(array_key_exists((string)$item->menu_item_parent,$result)) { | |
$result[$item->menu_item_parent]['children'][] = ['title'=>$item->title,'url'=>$item->url]; | |
} else { | |
$result[$item->ID] = ['parent'=>$item->title,'url'=>$item->url]; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment