Retrieve all menus and items in wordpress
Question
Is it posible with a single REST call to get all the menus and items inside them?
I’m just doing it 1 by 1 and that slows the rendering time. Thats what I have for just one menu:
function get_menu($data)
{
$args = array(
'menu' => $data['slug'],
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu-'.$data['slug'],
'menu_id' => 'menu-'.$data['slug'],
'echo' => false,
'fallback_cb' => false,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'item_spacing' => 'preserve',
'depth' => 0,
'walker' => '',
'theme_location' => '',
);
return array('menu' => wp_nav_menu($args));
}
add_action('rest_api_init', function () {
register_rest_route('wp/v2', '/menus/(?P<slug>[-w]+)', array(
'methods' => 'GET',
'callback' => 'get_menu'
));
});
Is there any other ways for doing that? Thanks in advance!
0
2 months
0 Answers
9 views
0
Leave an answer
You must login or register to add a new answer .