php - How to direct top-level menu to second-level menu and have the top-level menu have unique name? -
goal
in wordpress, attempting have admin menus automatically create if in menu "appearance -> menus". this, able develop.
the part in having troubles due specificity. attempting have top-level menu direct second-level menu , have top-level menu maintain original name.
code
setup in "appearance -> menu"
callsigns -> alpha -> bravo -> charlie
the top-level menu "callsigns" has modified options. navigation label "alpha" , title attribute "callsigns".
functions.php
add_action('init', 'register_my_menus' ); function register_my_menus() { register_nav_menus( array('admin-menus' => 'admin menus')); } add_action('admin_menu', 'my_admin_menus'); function my_admin_menus() { $locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object($locations['admin-menus']); $pages = wp_get_nav_menu_items($menu->term_id); foreach ($pages $page) { if ($page->menu_item_parent == 0) add_menu_page( $page->title, $page->attr_title ? $page->attr_title : $page->title, 'read', sanitize_title($page->title), 'admin_page_format' ); else add_submenu_page( sanitize_title($page->menu_item_parent->title), // issue $page->title, $page->attr_title ? $page->attr_title : $page->title, 'read', sanitize_title($page->title), 'admin_page_format' ); } }
i marked line confident issue. originally, did not try use "->" , of now, confident not possible use "->" twice in row did above.
incorrect solutions
sanitize_title($page->menu_item_parent->title)
the top-level menu works. has correct name , directs correct second-level menu. however, second-level menus not show admin menu @ all.
sanitize_title(get_the_title($page->menu_item_parent))
same outcome above incorrect solution.
functions.php
add_action('init', 'register_my_menus' ); function register_my_menus() { register_nav_menus( array('admin-menus' => 'admin menus')); } add_action('admin_menu', 'my_admin_menus'); function my_admin_menus() { $locations = get_nav_menu_locations(); $menu = wp_get_nav_menu_object($locations['admin-menus']); $pages = wp_get_nav_menu_items($menu->term_id); function get_menu_page($pages, $id) { foreach ($pages $page) if ($page->id == $id) return $page; } foreach ($pages $page) { if ($page->menu_item_parent == 0) add_menu_page( $page->title, $page->attr_title ? $page->attr_title : $page->title, 'read', sanitize_title($page->title), 'admin_page_format' ); else add_submenu_page( sanitize_title(get_menu_page($pages, $page->menu_item_parent)->title), $page->title, $page->attr_title ? $page->attr_title : $page->title, 'read', sanitize_title($page->title), 'admin_page_format' ); } }
the solution create function went menu loop again return object of page.
i doubt cleanest way thing come with.
Comments
Post a Comment