Since WordPress 3.0, menu management has been integrated, making it easy to create and manage (navigate) menus. Now, all you need to create and display a menu is a line of code( wp_nav_menu ), seems to have lost the space for adding things we need manually. For example, the menu function does not have any link to "return to home page" by default. Although it is easy to manually add a link to return to home page in the custom menu function, the link to return to home page is basically a necessary function of the website. Therefore, it is necessary to automatically add this function. There is a simpler way to use WordPress filters .
Using the navigation menu“ filters ”Function allows us to add specific menu items.
To draw inferences from one instance, after mining and research, add the following three pieces of code to the theme functions.php file, which will automatically add a login/logout link, a search box, and a link back to the home page to your WordPress 3.0 navigation menu.
1、 Add a login/logout link to your navigation menu
- add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
- function add_login_logout_link( $items , $args ) {
- ob_start();
- wp_loginout('index.php');
- $loginoutlink = ob_get_contents();
- ob_end_clean();
- $items .= '<li>'. $loginoutlink .'</li>';
- return $items ;
- }
explain:
First we add a function add_login_logout_link to the wp_nav_menu_items filter. Then, the ob_start, ob_get_contents and ob_end_clean (lines 4, 6 and 7) functions are “output Buffering” PHP functions that will “intercept” the information that would otherwise be sent to the browser. wp_loginout('index.php'); will add the logic and html code to login (if not logged in yet), or logout (if logged in). Since we don’t want to send that code to the browser yet, we “capture” the output (using ob_get_contents) in a variable ($searchform), and finally include that variable as a list item in the menu.
2、 Automatically add a search box to the navigation menu
- add_filter('wp_nav_menu_items','add_search_box', 10, 2);
- function add_search_box( $items , $args ) {
- ob_start();
- get_search_form();
- $searchform = ob_get_contents();
- ob_end_clean();
- $items .= '<li>' . $searchform . '</li>';
- return $items ;
- }
Create your own search template
Add_search_box uses the default searchform menu bar. But this may not be an ideal layout (maybe it contains the previous text "Search:" and "Search" key), then you should create a template file searchform.php in your theme template directory, and add the following code: The add_search_box function uses the default searchform template. But this may not be an ideal layout (maybe it contains the previous text "Search:" And a "Search" button), so you can create your own searchform.php template file, place it in your theme template directory, and add the following code:
- <form method= "get" class = "search-form" id= "search-form" action= "<?php bloginfo( 'home' ); ?>/" >
- <div class = "formfield" >
- <input class = "formInputText" type= "text" name= "s" id= "search-text" value= "Search ..." size= "12" maxlength= "16" tabindex= "1" onfocus= "if (this.value == 'Search ...') {this.value = '';}" onblur= "if (this.value == '') {this.value = 'Search ...';}" />
- </div>
- </form>
Alternatively, you can add form styles to match your navigation style, such as:
- input.formInputText {
- margin-top : 7px ;
- color : #666 ;
- padding : 3px ;
- background : #ccc ;
- }
- input.formInputText:hover {
- cursor : help ;
- color : 555;
- background : #ccc ;
- }
3、 Add a home page link to your navigation menu
- add_filter( 'wp_nav_menu_items', 'add_home_link', 10, 2 );
- function add_home_link( $items , $args ) {
- if (is_front_page())
- $class = ' class = "current_page_item" ';
- else
- $class = '' ;
- $homeMenuItem =
- '<li ' . $class . '>' .
- $args ->before .
- '<a href= "' . home_url( '/' ) . '" title= "Home" >' .
- $args ->link_before . 'Home' . $args ->link_after .
- '</a>' .
- $args ->after .
- '</li>';
- $items = $homeMenuItem . $items ;
- return $items ;
- }
Only add the above new items in specific locations
New items will be displayed in all custom menus by default, which may not be what you want, so you need to add a condition code so that the above code can only be executed in a specific menu location.
- function add_login_logout_link( $items , $args ) {
- if ( $args ->theme_location == 'Primary') {
- ob_start();
- wp_loginout('index.php');
- $loginoutlink = ob_get_contents();
- ob_end_clean();
- $items .= '<li>'. $loginoutlink .'</li>';
- }
- return $items ;
- }
The translation level is limited and there are some inaccuracies. Please forgive me. The above codes are all valid after testing.
Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!