Home> CMS Tutorial> WordPress> body text

How to create a theme navigation menu in WordPress (1)

青灯夜游
Release: 2023-02-22 21:10:34
forward
1881 people have browsed it

How to create a theme navigation menu in WordPress? The following article will share with you several methods of making WordPress theme navigation menu. I hope it will be helpful to you!

How to create a theme navigation menu in WordPress (1)

In the production of WordPress themes, the production of navigation menus is a key point. The HTML code of the navigation menu has been written. How to dynamically call it in the WordPress theme? This article will introduce you to several methods of writing PHP code to dynamically implement navigation. This article will only focus on the development of dynamic code and will not teach you how to write HTML, CSS and Javascript to implement a gorgeous navigation menu.

The production of WP 3.0 custom menu

Versions after WordPress 3.0 begin to support custom dynamic menus. The so-called dynamic menus allow users to decide which items to use. Add the item to the navigation menu, enter themanagement background of WordPress - Appearance - Menucolumn, and create your own menu by dragging the corresponding column. This is a happy thing for both WordPress theme developers and users. To implement a custom menu, the function you need to use iswp_nav_menu(). Passing some parameters to this function can output the custom menu. Let’s briefly talk about how to use this function.

First, add the following menu registration code between the in functions.php in the theme directory, so that you can use the wp_nav_menu function in the theme file :

// This theme uses wp_nav_menu() in one location. register_nav_menus();
Copy after login

Then we call wp_nav_menu() on the theme’s navigation bar to output the navigation menu HTML code:

 'mymenu', 'depth' => 1) ); ?>
Copy after login

The above code The format of the output HTML code is as follows:

Copy after login

The li items listed here are the columns you added inBackstage-Appearance-Menu, if you are not in the background yet Add a menu and the navigation bar will list all pages. In addition, wp_nav_menu will add a class to each li. Different classes mark the attributes of this menu item. For example, if an article page is currently opened,Category Ais the category to which this article belongs, thenThe li where category Ais located will look like the following code:

  • 分类A
  • Copy after login

    If it is on the home page, then the li of the menu item on the home page may look like this:

    Copy after login

    You can know their functions from the names of these classes. By adding css attributes to these classes, you can achieve the purpose of highlighting the current navigation menu, such as defining the current menu link in red:

    .current-post-ancestor a, .current-menu-parent a, .current-menu-item a, .current_page_item a { color: red; }
    Copy after login

    Okay, calling the custom menu of WordPress 3.0 is so simple. wp_nav_menu also has many parameters, such as custom ul node, id and class parameters of the ul parent node, etc.

    Use categories and pages as navigation bars

    Before WordPress 3.0, most WordPress themes used pages as navigation bars, and you could only add pages in the navigation. Doesn't seem free enough. When I first started using WordPress 2.7, I was worried about this problem. Finally, I looked through the documentation, checked some information, and realized adding categories in the navigation. For details, please see the article I wrote before:WordPress categories as navigation bar. And highlight

    Production of unconventional navigation bar

    The two methods mentioned above are all implemented using WordPress’s own functions. The HTML codes they input are also limited, using the form of ul li to build the menu list: such as:

    • ...
    • ...
    Copy after login

    If the front-end code of the theme is not written by you, and the navigation The code of the column is very confusing. This is not the WordPress standard ul navigation bar form above, such as the following code:

    标题
    菜单A
    菜单B
    Copy after login

    Rewrite the front-end code? I don’t think anyone is willing to do this, so what should we do? Also, what if the navigation bar does not use categories and pages, and does not allow the use of custom menus? In actual applications, we will also encounter various and strange requirements. We will continue to explore this issue in the next issue!

    Recommended learning: "WordPress Tutorial"

    The above is the detailed content of How to create a theme navigation menu in WordPress (1). For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:ludou.org
    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
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!