Answer the question
In order to leave comments, you need to log in
How is WordPress navigation menu implemented?
I need the essence of the implementation, I want to do something similar
While I'm stuck on this code:
It's not clear how to nest a child menu, and how to close tags.
$nav_menuA = array(
array('id' => 12, 'item_parent' => 0, 'name' => 'primary_page', 'page' => 'primary_index.php'),
array('id' => 15 , 'item_parent' => 12, 'name' => 'page1', 'page' => 'index1.php'),
array('id' => 19, 'item_parent' => 0, 'name' => 'page4', 'page' => 'index4.php'),
);
$default = array('items' => '[ul class="
foreach($nav_menuA as $key => $item){
$sorted_menu_items[$key] = $item;
if($item['item_parent'])
$menu_items_with_children[ $item['item_parent'] ] = true;
}
if ( $menu_items_with_children ) {
foreach ( $sorted_menu_items as &$item ) {
if( isset( $menu_items_with_children[ $item['id'] ] ) ){
$output .= "[li][ul class=\"sub -menu\"]{$item['name']}[/ul][/li]";
}else{
$output .= '[li]'.$item['name'];
}
}
}
echo sprintf($format, $output);
Answer the question
In order to leave comments, you need to log in
If you really need the essence, then you need to read the WordPress code. On the other hand, if there are standard solutions in the form of wp_nav_menu() , why invent a bicycle... Read wp-kama.ru/function/wp_nav_menu there are also excerpts from the code.
https://gist.github.com/hitautodestruct/4345363 this code helped me many times.
I just remade it for myself, or, you can look in the direction of the Walker Menu.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question