Home >CMS Tutorial >WordPress >How to use wp_nav_menu in wordpress

How to use wp_nav_menu in wordpress

藏色散人
藏色散人Original
2020-01-10 09:48:422901browse

How to use wp_nav_menu in wordpress

#How to use wp_nav_menu in wordpress?

wordpress wp_nav_menu usage instructions

Recommended: "wordpress tutorial"

wp_nav_menu() method is located in wp -includes/nav-menu-templates.php file.

Its main purpose is to use this method to

realize the background generation menu call.

Before using this function, the theme 3.0 menu function must be activated.

The method is as follows:

Add

add_theme_support( 'nav-menus' );或者 
•// 自定义菜单 
•register_nav_menus( 
•array( 
•‘header-menu’ => __( ’导航自定义菜单’ ), 
•‘footer-menu’ => __( ’页角自定义菜单’ ) 
•) 
•);

to the functions.php file and simply call it as follows:

<?php wp_nav_menu($args);?>

The default layout of the called menu is

The code is as follows:

<?php $defaults = array( 
&#39;theme_location&#39; => , 
&#39;menu&#39; => , 
&#39;container&#39; => &#39;div&#39;, 
&#39;container_class&#39; => &#39;menu-{menu slug}-container&#39;, 
&#39;container_id&#39; => , 
&#39;menu_class&#39; => &#39;menu&#39;, 
&#39;menu_id&#39; => , 
&#39;echo&#39; => true, 
&#39;fallback_cb&#39; => &#39;wp_page_menu&#39;, 
&#39;before&#39; => , 
&#39;after&#39; => , 
&#39;link_before&#39; => , 
&#39;link_after&#39; => , 
&#39;depth&#39; => 0, 
&#39;walker&#39; => ); 
?>

If there are multiple menus, call the following

<?php echo wp_nav_menu( array( &#39;container_class&#39; => &#39;menu-header&#39;, &#39;theme_location&#39; => &#39;primary&#39; ) ) ?>

The menu bar will be generated differently depending on whether you are logged in or not

<?php
if ( is_user_logged_in() ) {
wp_nav_menu( array( &#39;theme_location&#39; => &#39;logged-in-menu&#39; ) );
} else {
wp_nav_menu( array( &#39;theme_location&#39; => &#39;logged-out-menu&#39; ) );
}
?>

Remove the menu bar

<?php
function my_wp_nav_menu_args( $args = &#39;&#39; )
{
$args[&#39;container&#39;] = false;
return $args;
} // function
add_filter( &#39;wp_nav_menu_args&#39;, &#39;my_wp_nav_menu_args&#39; );
?>
The menu css style generated by

or

<?php wp_nav_menu( array( &#39;container&#39; => &#39;&#39; ) ); ?>

is

. You can add the tags used through

&#39;before&#39;          => ,<BR>  &#39;after&#39;           => ,<BR>  &#39;link_before&#39;     => ,<BR>  &#39;link_after&#39;      => ,<BR>

and beautify them with css, so that you can get what you want. desired effect.

The above is the detailed content of How to use wp_nav_menu in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn