Home >CMS Tutorial >WordPress >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( 'theme_location' => , 'menu' => , 'container' => 'div', 'container_class' => 'menu-{menu slug}-container', 'container_id' => , 'menu_class' => 'menu', 'menu_id' => , 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => , 'after' => , 'link_before' => , 'link_after' => , 'depth' => 0, 'walker' => ); ?>
If there are multiple menus, call the following
<?php echo wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ) ?>
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( 'theme_location' => 'logged-in-menu' ) ); } else { wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) ); } ?>
Remove the menu bar
<?php function my_wp_nav_menu_args( $args = '' ) { $args['container'] = false; return $args; } // function add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' ); ?>The menu css style generated by
or
<?php wp_nav_menu( array( 'container' => '' ) ); ?>
is
. You can add the tags used through
'before' => ,<BR> 'after' => ,<BR> 'link_before' => ,<BR> 'link_after' => ,<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!