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

How to use wp_nav_menu in wordpress

藏色散人
Release: 2020-01-10 09:48:42
Original
2913 people have browsed it

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’ => __( ’页角自定义菜单’ ) 
•) 
•);
Copy after login

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

<?php wp_nav_menu($args);?>
Copy after login

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; => ); 
?>
Copy after login

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; ) ) ?>
Copy after login

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; ) );
}
?>
Copy after login

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; );
?>
Copy after login
The menu css style generated by

or

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

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template