Advanced WordPress 3.0 menu functions

WordPress forty-two 13.6K Reading mode

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

  1. add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
  2. function add_login_logout_link( $items , $args ) {
  3. ob_start();
  4. wp_loginout('index.php');
  5. $loginoutlink = ob_get_contents();
  6. ob_end_clean();
  7. $items .= '<li>'. $loginoutlink .'</li>';
  8. return $items ;
  9. }

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

  1. add_filter('wp_nav_menu_items','add_search_box', 10, 2);
  2. function add_search_box( $items , $args ) {
  3. ob_start();
  4. get_search_form();
  5. $searchform = ob_get_contents();
  6. ob_end_clean();
  7. $items .= '<li>' . $searchform . '</li>';
  8. return $items ;
  9. }

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:

  1. <form method= "get" class = "search-form" id= "search-form" action= "<?php bloginfo( 'home' ); ?>/" >
  2. <div class = "formfield" >
  3. <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 ...';}" />
  4. </div>
  5. </form>

Alternatively, you can add form styles to match your navigation style, such as:

  1. input.formInputText {
  2. margin-top : 7px ;
  3. color : #666 ;
  4. padding : 3px ;
  5. background : #ccc ;
  6. }
  7. input.formInputText:hover {
  8. cursor : help ;
  9. color : 555;
  10. background : #ccc ;
  11. }

3、 Add a home page link to your navigation menu

  1. add_filter( 'wp_nav_menu_items', 'add_home_link', 10, 2 );
  2. function add_home_link( $items , $args ) {
  3. if (is_front_page())
  4. $class = ' class = "current_page_item" ';
  5. else
  6. $class = '' ;
  7. $homeMenuItem =
  8. '<li ' . $class . '>' .
  9. $args ->before .
  10. '<a href= "' . home_url( '/' ) . '" title= "Home" >' .
  11. $args ->link_before .  'Home' . $args ->link_after .
  12. '</a>' .
  13. $args ->after .
  14. '</li>';
  15. $items = $homeMenuItem . $items ;
  16. return $items ;
  17. }

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.

  1. function add_login_logout_link( $items , $args ) {
  2. if ( $args ->theme_location == 'Primary') {
  3. ob_start();
  4. wp_loginout('index.php');
  5. $loginoutlink = ob_get_contents();
  6. ob_end_clean();
  7. $items .= '<li>'. $loginoutlink .'</li>';
  8. }
  9. return $items ;
  10. }

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!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments forty-two    Visitors twenty-eight    Author fourteen
    •  Falling down de Fangfei
      Falling down de Fangfei five

      Take a decisive seat on the sofa, make a mark and look at it slowly

      •  the republic of korea
        the republic of korea five

        I want to ask Brother Bird how to solve the problem of garbled Chinese in the PHP file.

          •  Robin
            Robin

            @ the republic of korea You should have used the wrong editor when editing the Chinese template
            After modifying the Chinese template, the default code should be changed to: UTF-8 without BOM

          •  Little fart
            Little fart six

            Hey hey, Brother Bird has started to do the tutorial! It's great

              •  Robin
                Robin

                @ Little fart Khan, where did I write this tutorial? It has the original address
                Most of the WP tutorials that can be seen in China are copied from foreign ones, which are fake and original

                •  Apple
                  Apple five

                  @ Little fart Hehe, the original tutorial is also pseudo original

                    •  Robin
                      Robin

                      @ Apple Yes, foreign language is good. Look at the technical articles written by foreigners and bring them back

                  •  lady
                    lady zero

                    Hello
                    I use the template on this website
                    But why are the descriptions and keywords on my website like this

                    Plug in I have added a<! – All in One SEO Pack

                    What is the reason?
                    Sorry to bother you.

                      •  Robin
                        Robin

                        @ lady Theme has integrated SEO function
                        You don't need that plug-in

                      •  Division
                        Division three

                        Brother Bird, please tell me how to indent two characters automatically. I forgot where to change it.

                        •  kael
                          kael seven

                          Brother Bird, the lantern on the upper right corner of the theme is not displayed, and I will not display it if I change to another material~Solve~

                            •  kael
                              kael seven

                              @ kael I found the reason. The reason for my own computer~

                            •  → ☆ [Dark] ☆←
                              → ☆ [Dark] ☆← five

                              Wait for the official version of 3.1, wait for the official release of the new theme, and then change it, hehe.

                              •  Mr. Bean
                                Mr. Bean six

                                I wonder if Brother Bird has considered adding the function of submission page?

                                  •  Robin
                                    Robin

                                    @ Mr. Bean It seems that there are plug-in contributors

                                      •  Mr. Bean
                                        Mr. Bean six

                                        @ Robin What plug-in? Can you tell me? The most important thing is whether you consider integration

                                          •  Robin
                                            Robin

                                            @ Mr. Bean submit_posts
                                            Very good features
                                            http://zmingcx.com/contributors
                                            But I don't think it's necessary. It's not as convenient as sending email directly

                                              •  Mr. Bean
                                                Mr. Bean six

                                                @ Robin In fact, many people have this necessity... Brother Bird may consider

                                                  •  Robin
                                                    Robin

                                                    @ Mr. Bean Well, I have time to do one

                                                    •  Mr. Bean
                                                      Mr. Bean six

                                                      @ Robin Thank you, Brother Bird!! I forgot to tell you that you are also one of my idols

                                                      •  Robin
                                                        Robin

                                                        @ Mr. Bean As long as it's not nausea

                                                        •  Deduct words when something happens
                                                          Deduct words when something happens zero

                                                          @ Robin Brother Bird, can you show me how to associate the pages in WordPress with the category directory? Can you leave a Q or something? If you want to leave a message in Q space, please!!

                                                •  lmdtw
                                                  lmdtw zero

                                                  Mars problem: where should the code be added

                                                    •  Robin
                                                      Robin

                                                      @ lmdtw It seems that this article is not written in Martian, and it is clearly added to the functions.php file

                                                    •  Chenguang Blog
                                                      Morninglight Blog five

                                                      Thank you, Brother Bird!

                                                      •  51open
                                                        51open one

                                                        How to add your style, like the navigation menu of your website?

                                                          •  Robin
                                                            Robin

                                                            @ 51open http://zmingcx.com/wordpress3-0-using-the-navigation-menu-graphic-tutorials.html

                                                              •  51open
                                                                51open one

                                                                @ Robin Thank you, Brother Bird. It's settled now

                                                            •  Enduring
                                                              Enduring zero

                                                              Brother Bird, which php page was added to your message board ["Tips! If you have a question about the use of the theme, please refer to: HotNews pro Version 2.4 Use Question Summary, Installation and Use Need to Know!]??

                                                              •  Solovia
                                                                Solovia two

                                                                Come on, old bird!!!!!!!!!!!!!!!!!!!!!
                                                                Hotnews pro2.4 has a bug leaked from the website installation directory
                                                                Today, during the GZIP acceleration of the start space, I accidentally found that the website directory of this topic was leaked.
                                                                By checking the source code on the home page, we can see the theme installation directory, CSS call directory and call file (even directory guessing and traversal are not required...) Can this be improved?
                                                                Although it doesn't matter to the webmaster, I think it is better to change it for the sake of code perfection or security cleanliness. what you think? Brother Bird!
                                                                One more sentence: I have been tossing about the topic code of Brother Bird for three days, and it is quite beautiful. The code is concise and the comments are detailed. The problem index.php is awkward. After reading it for a long time, the first sentence is not understood, and the judgment switch is not found in the source code of the topic My cup!
                                                                Brother Bird, don't hit me with a brick... Take a screenshot of the directory on Brother Bird's website. For safety, only part of it will be taken.
                                                                When visiting the following website: domain/wp content/themes/HotNewspro24, the installation directory leaks when reading index.php by default.
                                                                [img] http://www.shelinbian.com/files/201102/2011-02-26-11390159.jpg [/img]

                                                                  •  Robin
                                                                    Robin

                                                                    @ Solovia No research on website security, please let me know if there is any solution

                                                                  •  funbean
                                                                    funbean one

                                                                    Excuse me, I'm using robin navigation, which is the same as the current main site, but how can only two live or die navigation come out? Add one more and the first one will disappear....

                                                                    •  east
                                                                      east zero

                                                                      Hello, I'm a novice, and I don't know how to modify the code. My website is a wp program that uses someone else's template, but that template can't add pages. Even if the page is added, it can't be displayed. What should I do about this? Because no good template has been found, I am using it.

                                                                      •  Temperature and humidity meter
                                                                        Temperature and humidity meter one

                                                                        Brother Bird, how to add description and keyword to each category page? The All in One Seo Pack cannot be installed.

                                                                        •  lp
                                                                          lp zero

                                                                          I want to add an article directly under the new page. Why???

                                                                          •  ziranniao
                                                                            ziranniao three

                                                                            I want to add an article directly under the new page. Why???
                                                                            I also want to know how to do this. Please help Brother Bird!!!!!!!!!!!!!!!!!!

                                                                              •  Robin
                                                                                Robin

                                                                                @ ziranniao The page is different from the log article. The page itself is an article. I don't know what you added

                                                                                  •  Save money by opening stores
                                                                                    Save money by opening stores three

                                                                                    @ Robin He said that the new page can call articles at will. The method is as follows: 1. Copy the index.php file in the theme directly
                                                                                    2: Open this file and add the following code at the top of the code:

                                                                                    Name: adsense is the template name, which can be arbitrary, and should be the same as the new file name

                                                                                    3: Next, we need to implement this template to display the content of articles under a specific category. Search to find the following codes: (similar ones are enough)

                                                                                    Add:

                                                                                    Cat=1 is the ID of the category directory. When you select this category when publishing an article, the article will automatically go to the new page
                                                                                    =====My question=====
                                                                                    After I changed from blog to CMS theme, the previously created page is useless after clicking. It is the same as the home page, but the title and URL are different. Netbird solves the problem. The address is: http://www.yeweiqing.com

                                                                                •  long_jp
                                                                                  long_jp zero

                                                                                  I like the structure and theme of your website. How did you do it? Can you share your theme with me? After I downloaded and installed WordPress, there was nothing. I just wanted to have a navigation directory, but I couldn't do anything. Please tell me how to do it, thank you!

                                                                                  •  Mr. li
                                                                                    Mr. li one

                                                                                    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 500001 bytes) in/data/multiserv/users/907431/projects/2103397/www/wp includes/class snoopy.php on line 900

                                                                                   anonymous

                                                                                  Comment

                                                                                  Anonymous netizens
                                                                                   :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

                                                                                  Drag the slider to complete validation