In the blog post before last, I wrote about an interesting innovation in the latest version of wordpress - a special mechanism for creating and managing. Now it has become much more convenient and easier for ordinary users to create menus of various complexity, which can consist not only of blog pages or categories, but also have links to any URL. To display the menu in the template, a special function wp_nav_menu is used - I'll tell you about it today.

If there is no menu section in the WordPress admin area, then it can be activated by adding a special code to the functions.php file

Here first is the name of the menu we created. This is the use of a function in the general case without widgets, you will need to work with them a little differently there. Nevertheless, the wp_nav_menu function can be displayed without arguments, as a result of which different situations will be "viewed" - first a match by the name of the menu, if at least one menu item is specified for it, otherwise a non-empty menu will simply be displayed, etc. ... But again, I advise you to just use the above code and then not figure out what the function should output without arguments. Its syntax is as follows:

The following parameters are used here:

$ menu- the selected identifier for the menu - ID, slug or menu name.

$ container- the UL menu is "wrapped" in a DIV container by default with this setting.

$ container_class- indicates the class of the container, by default its value is menu- (menu slug) -container, that is, in our case, for example, there will be a class menu-first-container.

$ container_id- you can add an ID to the container, not specified by default.

$ menu_class Is the class for the UL menu item, its value is menu.

$ menu_id- ID for the ul element, defaults to menu- (slug)

$ echo- if you do not want to display the menu, but return the value of the function, use the value 0 for this setting.

$ fallback_cb- if the menu does not exist, the wp_page_menu function is called.

$ before- sets the text that is displayed before link A.

$ link_before- displays the phrase before the link text, not specified.

$ link_after- displayed after the link text, also empty.

$ depth- sets the number of hierarchy levels for displaying the menu, the default value 0 displays the entire menu.

$ walker- some incomprehensible user "walker object", probably more needed by advanced developers.

$ theme_location- the location of the theme, where the menu will be used, must be activated via register_nav_menu () in order for the user to be able to select it. Also some not entirely clear setting, apparently, when working with widgets.

Examples of using the wp_nav_menu function

The simplest code given in the code is:

Removing the DIV container from the menu

"")); ?>

Basically, there is nothing difficult in creating and managing a wordpress 3.0 menu. The developers have greatly simplified the work procedure and expanded the capabilities of this navigation element. The solution is often used in many template tasks, for example, when creating for mobile and desktop versions. I'll add a couple more snippets on the topic later.

P.S. The sentry. An interesting and useful SEO blog for webmasters, where you will find answers to your questions about seo.
The Aweb company has long and very well established itself in the field of website promotion, optimization and search engine optimization services on the Internet.

In this article, I will show you how you can create multilevel menu in PHP and MySQL... Of course, you can think of many options for its creation, but judging by the number of your questions on this topic, you need an example. And I will give it in this article. I note right away that this article makes sense only for those who know PHP and knows how to work with MySQL... Everyone else first needs to go through this one, or read some books on PHP and MySQL.

First, let's create a table in the database with the following fields:

  • id- unique identificator.
  • title- anchor links in the menu.
  • link- the address to which the menu item will lead.
  • parent_id- parent ID. If there is no parent item, then it will be NULL (or you can put another 0).

The table has been sorted out, now it's time PHP code... Full Php code is shown below:

$ mysqli = new mysqli ("localhost", "root", "", "db"); // Connect to the database
$ result_set = $ mysqli-> query ("SELECT * FROM` menu` "); // Make a selection of all records from the table with the menu
$ items = array (); // Array for menu items
while (($ row = $ result_set-> fetch_assoc ())! = false) $ items [$ row ["id"]] = $ row; // Fill the array with a selection from the database
$ childrens = array (); // Array for matching children to their parent
foreach ($ items as $ item) (
if ($ item ["parent_id"]) $ childrens [$ item ["id"]] = $ item ["parent_id"]; // Fill the array
}
function printItem ($ item, $ items, $ childrens) (
/ * Display the menu item * /
echo "

  • ";
    echo "". $ item ["title"]. "";
    $ ul = false; // Have children been rendered?
    while (true) (
    / * An infinite loop in which we search for all children * /
    $ key = array_search ($ item ["id"], $ childrens); // Looking for a child element
    if (! $ key) (
    / * No children found * /
    if ($ ul) echo ""; // If the child elements were displayed, then close the list
    break; // Exit the loop
    }
    unset ($ childrens [$ key]); // Remove the found element (so that it is not displayed again)
    if (! $ ul) (
    echo "
      "; // Start the internal list if there were no children yet
      $ ul = true; // Set the flag
      }
      echo printItem ($ items [$ key], $ items, $ childrens); // Recursively display all children
      }
      echo "";
      }
      ?>

      This code is completely working, however, you should understand that no one writes like this (in particular, output through echo HTML tags). And your task is to take the algorithm from this code, but not the code itself. And then connect this algorithm to your engine. I have tried to comment carefully on the output code multilevel menu in PHP and MySQL, but, of course, it is not the most transparent and already requires quite good initial knowledge. If you still do not know well PHP and MySQL, then first I strongly recommend that you go through this

      No site is complete without navigation, or as it is also called "site menu". So the site menu can be single-level and multi-level in the form of a tree. If with a single-level menu there are no special difficulties in terms of implementation, then when creating a multi-level menu you need to think carefully.

      The most important thing in this task is to design the database for our multilevel menu. Let's create a Categories table with three fields id, title, parent where:

      • ID- identifier
      • Title- Menu name
      • Parent- Parent category by default 0

      The field is responsible for branching the menu Parent if Parent = 0, then this category is the parent. In order to add children to the parent category, you need to specify in the parent field ID the desired parent. For instance:

      Tables with categories

      As you can see from the table, the parent category has Cars there are two descendants - this Mazda and Honda field related Parent... And the category Motorcycles two descendants are Kawasaki and Harley... However, the Boats category has no descendants. Hope you figured out how to link categories.

      Next, we move from words to practice. Let's create the Categories table.

      CREATE TABLE IF NOT EXISTS `categories` (` id` int (10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar (255) NOT NULL,` parent` int (10) unsigned NOT NULL, PRIMARY KEY (`id`)) ENGINE = MyISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT = 20; - - Dump table data `categories` - INSERT INTO` categories` (`id`,` title`, `parent`) VALUES (1," Cars ", 0), (2," Motorcycles ", 0) , (3, Mazda, 1), (4, Honda, 1), (5, Kawasaki, 2), (6, Harley, 2), (7, Mazda 3, 3 ), (8, Mazda 6, 3), (9, Sedan, 7), (10, Hatchback, 7), (11, Boats, 0), (12, Liftback, 8), (13, "Crossover", 8), (14, "White", 13), (15, "Red", 13), (16, "Black", 13), (17, "Green", 13), (18, Mazda CX, 3), (19, Mazda MX, 3);

      The work algorithm consists of the following:

      Create a database connection

      query ("SET NAMES" utf8 ""); / * * This is the "official" object-oriented way to do it * however $ connect_error did not work until PHP 5.2.9 and 5.3.0. * / if ($ mysqli-> connect_error) (die ("Connection error (". $ mysqli-> connect_errno. ")". $ mysqli-> connect_error);) / * * If you need to be sure of compatibility with versions before 5.2 .9, * better to use code like this * / if (mysqli_connect_error ()) (die ("Connection error (". Mysqli_connect_errno (). ")". Mysqli_connect_error ());)

      Writing a function to get data from the Categories table

      // Get the array of our menu from the database as an array function getCat ($ mysqli) ($ sql = "SELECT * FROM` categories` "; $ res = $ mysqli-> query ($ sql); // Create an array where the key of the array is the menu ID $ cat = array (); while ($ row = $ res-> fetch_assoc ()) ($ cat [$ row ["id"]] = $ row;) return $ cat;)

      We get an array like this, where the array key is the category ID.

      Solid Wood Build Function by Tommy Lacroix

      // Function of building a tree from an array from Tommy Lacroix function getTree ($ dataset) ($ tree = array (); foreach ($ dataset as $ id => & $ node) (// If there are no attachments if (! $ Node [" parent "]) ($ tree [$ id] = & $ node;) else (// If there are children then iterate over the $ dataset array [$ node [" parent "]] [" childs "] [$ id] = & $ node;)) return $ tree;)

      We get an array in the form of a tree

      Entire script

      query ("SET NAMES" utf8 ""); / * * This is the "official" object-oriented way to do it * however $ connect_error did not work until PHP 5.2.9 and 5.3.0. * / if ($ mysqli-> connect_error) (die ("Connection error (". $ mysqli-> connect_errno. ")". $ mysqli-> connect_error);) / * * If you need to be sure of compatibility with versions before 5.2 .9, * it is better to use this code * / if (mysqli_connect_error ()) (die ("Connection error (". Mysqli_connect_errno (). ")". Mysqli_connect_error ());) // Get the array of our menu from the database as an array function getCat ($ mysqli) ($ sql = "SELECT * FROM` categories` "; $ res = $ mysqli-> query ($ sql); // Create an array where the array key is the menu ID $ cat = array (); while ($ row = $ res-> fetch_assoc ()) ($ cat [$ row ["id"]] = $ row;) return $ cat;) // Tommy Lacroix array tree building function function getTree ($ dataset) ($ tree = array (); foreach ($ dataset as $ id => & $ node) (// If there are no attachments if (! $ node ["parent"]) ($ tree [$ id] = & $ node; ) else (// If there are children, then iterate over the $ dataset array [$ node ["parent"]] ["childs"] [$ id] = & $ node;)) return $ tree;) // Get prepared th data array $ cat = getCat ($ mysqli); // Create a tree menu $ tree = getTree ($ cat); // Template for displaying the menu in the form of a tree function tplMenu ($ category) ($ menu = "
    • ". $ category [" title "]." "; if (isset ($ category [" childs "])) ($ menu. ="
        ". showCat ($ category [" childs "])."
      ";) $ menu. ="
    • "; return $ menu;) / ** * Recursively read our template ** / function showCat ($ data) ($ string =" "; foreach ($ data as $ item) ($ string. = tplMenu ($ item); ) return $ string;) // Get HTML markup $ cat_menu = showCat ($ tree); // Display echo "
        ". $ cat_menu."
      "; ?>

      Result of work

      Multilevel menu in PHP + MySQL for admin panel

      If you want to use this menu in the admin panel of your site, then you need to rewrite a couple of functions tplMenu (), showCat ().

      ". $ category [" title "]."";) else ($ menu =" ";) if (isset ($ category [" childs "])) ($ i = 1; for ($ j = 0; $ j< $i; $j++){ $str .= "→"; } $i++; $menu .= showCat($category["childs"], $str); } return $menu; } /** * Рекурсивно считываем наш шаблон **/ function showCat($data, $str){ $string = ""; $str = $str; foreach($data as $item){ $string .= tplMenu($item, $str); } return $string; } //Получаем HTML разметку $cat_menu = showCat($tree, ""); //Выводим на экран echo ""; ?>

      Result of work

      Choose Cars → Mazda → → Mazda 3 → → → Sedan → → → Hatchback → → Mazda 6 → → → Liftback → → → Crossover → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → → →>> Mazda CX → → Mazda MX → Honda Motorcycles → Kawasaki → Harley Boats

      Displays a custom menu created in the Appearance> Menus panel.

      Which navigation menu to display (there may be several of them) is specified in the theme_location parameter.

      If the theme_location parameter is not specified, then the menu for display will be selected in the following order:

        A menu matching the ID, slug or description passed in the "menu" parameter and if there is at least one link (one element) in this menu;

        otherwise, the first non-empty menu;

        or, will display the value returned by the function specified in the "fallback_cb" parameter (by default, the wp_page_menu function is specified there);

      1. if nothing fits, the function will not output anything.

      For a theme to support a menu, you need to enable this feature with:
      add_theme_support ("menus");

      Or you can register a place for the menu using register_nav_menu (), then the menu support by the theme will turn on automatically.

      Menu item change filters

      • add_filter ("wp_nav_menu_args", "my_wp_nav_menu_args"); function my_wp_nav_menu_args ($ args = "") ($ args ["container"] = false; return $ args;)

        # 4.1. Let's delete the container, only from one displayed menu

        "" ]); ?>

        # 5 Remove the ul wrapper

        This example will remove the ul tag wrapping from the menu:

        "% 3 $ s"]); ?>

        # 6 Add a word to the beginning of the menu

        This example shows how to add a word to the beginning of a menu list, as the same menu item, just not a link. Add the word "List" to the beginning of the menu, and also specify the id attribute for the created li tag:

        "primary", "items_wrap" => "

        • List:
        • % 3 $ s
        " ]); ?>

        # 7 add CSS classes to all menus

        Using a hook, we can add our own CSS classes if the condition we need is met.

        Add a CSS class if this is a post and the name of the menu item is "blog":

        Add_filter ("nav_menu_css_class", "special_nav_class", 10, 2); function special_nav_class ($ classes, $ item) (if (is_single () && $ item-> title == "(! LANG: Blog"){ $classes = "special-class"; } return $classes; } !}

        # 8 Using custom function to build menus

        First, you need to specify the argument "walker" => new Your_Walker_Function.

        Your_Walker_Function is our new class that builds the menu. In order not to reinvent the wheel, it can be copied from the original, see the Walker_Nav_Menu class. We copy the class code and just correct it where necessary.

        Here's an example adding menu depth and even / odd CSS classes to menu items (both ul and li):

        // your menu building class: class magomra_walker_nav_menu extends Walker_Nav_Menu (// add classes to ul sub-menus function start_lvl (& $ output, $ depth) (// depth dependent classes $ indent = ($ depth> 0? str_repeat ("\ t ", $ depth):" "); // code indent $ display_depth = ($ depth + 1); // because it counts the first submenu as 0 $ classes = array (" sub-menu ", ($ display_depth% 2? "Menu-odd": "menu-even"), ($ display_depth> = 2? "Sub-sub-menu": ""), "menu-depth-". $ Display_depth); $ class_names = implode ( "", $ classes); // build html $ output. = "\ n". $ indent. "

          "." \ n ";) // add main / sub classes to li" s and links function start_el (& $ output, $ item, $ depth, $ args) (global $ wp_query; $ indent = ($ depth> 0 ? str_repeat ("\ t", $ depth): ""); // code indent // depth dependent classes $ depth_classes = array (($ depth == 0? "main-menu-item": "sub-menu- item "), ($ depth> = 2?" sub-sub-menu-item ":" "), ($ depth% 2?" menu-item-odd ":" menu-item-even ")," menu -item-depth- ". $ depth); $ depth_class_names = esc_attr (implode (" ", $ depth_classes)); // passed classes $ classes = empty ($ item-> classes)? array (): (array) $ item-> classes; $ class_names = esc_attr (implode ("", apply_filters ("nav_menu_css_class", array_filter ($ classes), $ item))); // build html $ output. = $ indent. "
        • ) return $ items; ) function __nav_hasSub ($ item_id, $ items) (foreach ($ items as $ item) (if ($ item-> menu_item_parent && $ item-> menu_item_parent == $ item_id) return true;) return false;)

          # 11 Adding a class to individual menu items

          Since version 4.1.

          There is a special hook for this: nav_menu_css_class. And now classes can be added or removed through it. For example, let's add the class my__class to all menu items:

          Add_filter ("nav_menu_css_class", "add_my_class_to_nav_menu", 10, 2); function add_my_class_to_nav_menu ($ classes, $ item) (/ * $ classes contains Array (=> menu-item => menu-item-type-post_type => menu-item-object-page => menu-item-284) * / $ classes = "my__class"; return $ classes;)

          Prior to version 4.1.

          Classes for menu items are added by the function _wp_menu_item_classes_by_context (& $ menu_items); ... But unfortunately, it does not provide any filters to add your own class. Therefore, let's go the roundabout way and use the str_replace () crutch:

          // get but not display the menu $ menu = wp_nav_menu (array ("echo" => 0,)); // add the class my__class to all items $ menu = str_replace ("class =" menu-item "," class = "menu-item my__class", $ menu); // display echo $ menu;

          # 12 Show menu only if it exists

          By default, if there is no menu, the site pages will be displayed instead. But if you need to display the menu only when it was created in the admin panel, specify the fallback_cb parameter as "__return_empty_string":

          Wp_nav_menu (array ("theme_location" => "primary-menu", "fallback_cb" => "__return_empty_string"));

          # 13 Display only sub-menu item

          Let's say there is a first level and each of the elements of the first level has its own submenu. We need to display such a submenu for an item with the class menu-item-135:

          ## Cut out all LIs of the required submenu and display them in our UL block $ menu = wp_nav_menu (array ("theme_location" => "header_menu", "container" => "", "echo" => 0,)); $ regex_part = preg_quote ("menu-item-135"); // display the submenu of the "gotovye-resheniya" item preg_match ("~". $ regex_part. ". * sub-menu [^>] +> (. *?)

        ~ s ", $ menu, $ mm); if (! empty ($ mm)) echo" ";

        Not very optimal but working example. Sometimes it can be useful for low-traffic sites where you need to quickly get a result.

        CSS classes for menu items

        The following CSS classes are added to menu items (segregated by conditions on which pages the user is located):

        For all elements on all pages

          .menu-item- to all menu items;

          .menu-item-object- (object)- to all elements, where (object) is replaced by the name of the post type or taxonomy:
          .menu-item-object-category (for categories)
          .menu-item-object-tag (for tags)
          .menu-item-object-page (for persistent pages)
          .menu-item-object- (custom)

        • .menu-item-type- (type)- to all menu items, where (type) is replaced by the link type (post or taxonomy). Groups all types of links:
          .menu-item-type-post_type (persistent page, custom post type)
          .menu-item-type-taxonomy (category, label, or custom taxonomy)

        For elements of the current page

        • .current-menu-item- if the link in the menu matches the address of the page you are viewing. Current page.

        For parent elements for the page being viewed

        • .current-menu-parent
        • .current- (object) -ancestor
        • .current- (type) -ancestor

        For items somehow related to the page being viewed

        • .current-menu-ancestor
        • .current- (object) -ancestor
        • .current- (type) -ancestor

        For elements related to the main page of the site

        • .menu-item-home

        Compatible with wp_page_menu () function

        • .page_item
        • .page-item- $ ID
        • .current_page_item
        • .current_page_parent
        • .current_page_ancestor

        $ Item object

        Parameters $ item

        The examples often use the $ item menu item. Almost all the parameters of this element are shown below:

        Field Description
        ID Menu item ID
        menu_item_parent ID of the parent menu item
        classes array of menu item classes
        post_date date added
        post_modified date of last change
        post_author ID of the user who added this menu item
        title menu item title
        url menu item link
        attr_title title link attribute
        xfn rel link attribute
        target target link attribute
        current equals 1 if this is the current element
        current_item_ancestor 1 if the current item is a nested item
        current_item_parent 1 if the current element is the parent element
        menu_order serial number in the menu
        object_id The ID of the menu object. Entries, terms, etc.
        type menu item type (tax, entry)
        object tax name, post type: page, category, post_tag ...
        type_label localized type name: Category, Page
        post_parent Parent record ID
        post_title post title
        post_name record label
        Example object $ item
        WP_Post Object (=> 10 => 5 => 2019-02-11 13:33:39 => 2019-02-11 13:33:39 => => New => => publish => closed => closed = > => new => => => 2019-02-11 23:10:19 => 2019-02-11 23:10:19 => => 0 => http://dh5.com/?p= 10 => 1 => nav_menu_item => => 0 => raw => 10 => 0 => 10 => custom => custom => Custom Link => New => # => => => => Array = > extra-sub-menu => menu-item => menu-item-type-custom => menu-item-object-custom => => => =>)

        Example of using the walker parameter

        In the walker, you can specify an object that will build the menu. In this object, you can describe the HTML code of the resulting menu.

        If you need to create a menu for a non-standard layout, then sometimes it is easier to redo this object than to redo the layout.

        As an example of a walker object, take the Walker_Nav_Menu () class, which is the default. In it, we are only interested in one method, start_el (). It is he who is responsible for the HTML of each element. As a rule, it is enough to change only it. To do this, you need to create your own class that will extend the Walker_Nav_Menu class and specify it in the walker parameter when calling the menu.

        Let's look at an example. The code of the start_el () method is taken without changes. We use as a template:

        Class My_Walker_Nav_Menu extends Walker_Nav_Menu (/ ** * Starts the element output. * * @Since 3.0.0 * @since 4.4.0 The (@see "nav_menu_item_args") filter was added. * * @See Walker :: start_el () * * @param string $ output Passed by reference. Used to append additional content. * @param WP_Post $ item Menu item data object. * @param int $ depth Depth of menu item. Used for padding. * @param stdClass $ args An object of wp_nav_menu () arguments. * @param int $ id Current item ID. * / public function start_el (& $ output, $ item, $ depth = 0, $ args = array (), $ id = 0) (if (isset ($ args-> item_spacing) && "discard" === $ args-> item_spacing) ($ t = ""; $ n = "";) else ($ t = "\ t"; $ n = "\ n ";) $ indent = ($ depth)? str_repeat ($ t, $ depth):" "; $ classes = empty ($ item-> classes)? array (): (array) $ item-> classes; $ classes = "menu-item-". $ item-> ID; $ args = apply_filters ("nav_menu_item_args", $ args, $ item, $ depth); $ class_names = join ("", apply_filters ("nav_menu_css_class", array_ filter ($ classes), $ item, $ args, $ depth)); $ class_names = $ class_names? "class =" ". esc_attr ($ class_names)." "": ""; $ id = apply_filters ("nav_menu_item_id", "menu-item-". $ item-> ID, $ item, $ args, $ depth); $ id = $ id? "id =" ". esc_attr ($ id)." "": ""; // create the HTML code of the menu item $ output. = $ indent. " "; $ atts = array (); $ atts [" title "] =! empty ($ item-> attr_title)? $ item-> attr_title:" "; $ atts [" target "] =! empty ($ item- > target)? $ item-> target: ""; $ atts ["rel"] =! empty ($ item-> xfn)? $ item-> xfn: ""; $ atts ["href"] =! empty ($ item-> url)? $ item-> url: ""; $ atts = apply_filters ("nav_menu_link_attributes", $ atts, $ item, $ args, $ depth); $ attributes = ""; foreach ($ atts as $ attr => $ value) (if (! empty ($ value)) ($ value = ("href" === $ attr)? esc_url ($ value): esc_attr ($ value); $ attributes. = "" . $ attr. "=" ". $ value." "";)) $ title = apply_filters ("the_title", $ item-> title, $ item-> ID); $ title = apply_filters ("nav_menu_item_title", $ title, $ item, $ args, $ depth); $ item_output = $ args-> before; $ item_output. = " "; $ item_output. = $ args-> link_before. $ title. $ args-> link_after; $ item_output. =""; $ item_output. = $ args-> after; $ output. = apply_filters (" walker_nav_menu_start_el ", $ item_output, $ item, $ depth, $ args);))

        Now, when calling the menu, specify our walker:

        Wp_nav_menu (array ("theme_location" => "head_menu", "walker" => new My_Walker_Nav_Menu (),));

        Done, now each menu element will be built according to the HTML scheme we need.

        BEM menu using filters

        The layout will be formed according to the BEM methodology:

        File index.php or another to display the menu

        "header-menu",]);

        File functions.php

        "Top area", "footer-menu" => "Bottom area",]); )); // Changes the main parameters of the menu add_filter ("wp_nav_menu_args", "filter_wp_menu_args"); function filter_wp_menu_args ($ args) (if ($ args ["theme_location"] === "header-menu") ($ args ["container"] = false; $ args ["items_wrap"] = ""; $ args [ "menu_class"] = "menu menu - main menu-horizontal";) return $ args;) // Change the id attribute of the li tag add_filter ("nav_menu_item_id", "filter_menu_item_css_id", 10, 4); function filter_menu_item_css_id ($ menu_id, $ item, $ args, $ depth) (return $ args-> theme_location === "header-menu"? "": $ menu_id;) // Change the class attribute of the li tag add_filter ("nav_menu_css_class "," filter_nav_menu_css_classes ", 10, 4); function filter_nav_menu_css_classes ($ classes, $ item, $ args, $ depth) (if ($ args-> theme_location === "header-menu") ($ classes = ["menu-node", "menu-node - main_lvl_ ". ($ depth + 1)]; if ($ item-> current) ($ classes =" menu-node - active ";)) return $ classes;) // Changes the class of the nested ul add_filter (" nav_menu_submenu_css_class " , "filter_nav_menu_submenu_css_class", 10, 3); function filter_nav_menu_submenu_css_class ($ classes, $ args, $ depth) (if ($ args-> theme_location === "header-menu") ($ classes = ["menu", "menu - dropdown", "menu - vertical "];) return $ classes;) // Add classes to links add_filter (" nav_menu_link_attributes "," filter_nav_menu_link_attributes ", 10, 4); function filter_nav_menu_link_attributes ($ atts, $ item, $ args, $ depth) (if ($ args-> theme_location === "header-menu") ($ atts ["class"] = "menu-link"; if ($ item-> current) ($ atts ["class"]. = "menu-link - active";)) return $ atts;)

        Order very cheap likes on the public page on Facebook on this service and get the opportunity to choose not only a pleasant price, but also personal conditions for purchasing the resource. So, for example, you will have access to the choice of the speed mode of receipt and the quality of the signed pages. In addition, the service provides guarantees to its customers.

        The code wp nav menu: wp-includes / nav-menu-template.php WP 5.2.2

        "", "container" => "div", "container_class" => "", "container_id" => "", "menu_class" => "menu", "menu_id" => "", "echo" => true, "fallback_cb" => "wp_page_menu", "before" => "", "after" => "", "link_before" => "", "link_after" => "", "items_wrap" => "" , "item_spacing" => "preserve", "depth" => 0, "walker" => "", "theme_location" => "",); $ args = wp_parse_args ($ args, $ defaults); if (! in_array ($ args ["item_spacing"], array ("preserve", "discard"), true)) (// invalid value, fall back to default. $ args ["item_spacing"] = $ defaults [" item_spacing "];) / ** * Filters the arguments used to display a navigation menu. * * @since 3.0.0 * * @see wp_nav_menu () * * @param array $ args Array of wp_nav_menu () arguments. * / $ args = apply_filters ("wp_nav_menu_args", $ args); $ args = (object) $ args; / ** * Filters whether to short-circuit the wp_nav_menu () output. * * Returning a non-null value to the filter will short-circuit * wp_nav_menu (), echoing that value if $ args-> echo is true, * returning that value otherwise. * * @since 3.9.0 * * @see wp_nav_menu () * * @param string | null $ output Nav menu output to short-circuit with. Default null. * @param stdClass $ args An object containing wp_nav_menu () arguments. * / $ nav_menu = apply_filters ("pre_wp_nav_menu", null, $ args); if (null! == $ nav_menu) (if ($ args-> echo) (echo $ nav_menu; return;) return $ nav_menu;) // Get the nav menu based on the requested menu $ menu = wp_get_nav_menu_object ($ args- > menu); // Get the nav menu based on the theme_location if (! $ Menu && $ args-> theme_location && ($ locations = get_nav_menu_locations ()) && isset ($ locations [$ args-> theme_location])) ($ menu = wp_get_nav_menu_object ( $ locations [$ args-> theme_location]);) // get the first menu that has items if we still can "t find a menu if (! $ menu &&! $ args-> theme_location) ($ menus = wp_get_nav_menus () ; foreach ($ menus as $ menu_maybe) (if ($ menu_items = wp_get_nav_menu_items ($ menu_maybe-> term_id, array ("update_post_term_cache" => false))) ($ menu = $ menu_maybe; break;))) if (empty ( $ args-> menu)) ($ args-> menu = $ menu;) // If the menu exists, get its items. if ($ menu &&! is_wp_error ($ menu) &&! isset ($ menu_items)) ($ menu_items = wp_get_nav_menu_items ($ menu-> term_id, array ("update_post_term_cache" => false));) / * * If no menu was found: * - Fall back (if one was specified), or bail. * * If no menu items were found: * - Fall back, but only if no theme location was specified. * - Otherwise, bail. * / if ((! $ menu || is_wp_error ($ menu) || (isset ($ menu_items) && empty ($ menu_items) &&! $ args-> theme_location)) && isset ($ args-> fallback_cb) && $ args -> fallback_cb && is_callable ($ args-> fallback_cb)) (return call_user_func ($ args-> fallback_cb, (array) $ args);) if (! $ menu || is_wp_error ($ menu)) (return false;) $ nav_menu = $ items = ""; $ show_container = false; if ($ args-> container) (/ ** * Filters the list of HTML tags that are valid for use as menu containers. * * @since 3.0.0 * * @param array $ tags The acceptable HTML tags for use as menu containers. * Default is an array containing "div" and "nav". * / $ allowed_tags = apply_filters ("wp_nav_menu_container_allowedtags", array ("div", "nav")); if (is_string ($ args-> container) && in_array ($ args-> container, $ allowed_tags)) ($ show_container = true; $ class = $ args-> container_class? "class =" ". esc_attr ($ args-> container_class)." "": "class =" menu - ". $ menu-> slug." -container ""; $ id = $ args-> container_id? "id =" ". esc_attr ($ args-> container_id)." "": ""; $ nav_menu. = "<" . $args->container. $ id. $ class. ">"; )) // Set up the $ menu_item variables _wp_menu_item_classes_by_context ($ menu_items); $ sorted_menu_items = $ menu_items_with_children = array (); foreach ((array) $ menu_items as $ menu_item) ($ sorted_menu_items [$ menu_item-> menu_order] = $ menu_item; if ($ menu_item-> menu_item_parent) ($ menu_items_with_children [$ menu_item-> menu_item_parent) // true; Add the menu-item-has-children class where applicable if ($ menu_items_with_children) (foreach ($ sorted_menu_items as & $ menu_item) (if (isset ($ menu_items_with_children [$ menu_item-> ID])) ($ menu_item-> classes = "menu-item-has-children";))) unset ($ menu_items, $ menu_item); / ** * Filters the sorted list of menu item objects before generating the menu "s HTML. * * @Since 3.1.0 * * @param array $ sorted_menu_items The menu items, sorted by each menu item" s menu order. * @param stdClass $ args An object containing wp_nav_menu () arguments. * / $ sorted_menu_items = apply_filters ("wp_nav_menu_objects", $ sorted_menu_items, $ args); $ items. = walk_nav_menu_tree ($ sorted_menu_items, $ args-> depth, $ args); unset ($ sorted_menu_items); // Attributes if (! Empty ($ args-> menu_id)) ($ wrap_id = $ args-> menu_id;) else ($ wrap_id = "menu-". $ Menu-> slug; while (in_array ($ wrap_id, $ menu_id_slugs)) (if (preg_match ("# - (\ d +) $ #", $ wrap_id, $ matches)) ($ wrap_id = preg_replace ("# - (\ d +) $ #", "-". ++ $ matches, $ wrap_id);) else ($ wrap_id = $ wrap_id. "-1";))) $ menu_id_slugs = $ wrap_id; $ wrap_class = $ args-> menu_class? $ args-> menu_class: ""; / ** * Filters the HTML list content for navigation menus. * * @since 3.0.0 * * @see wp_nav_menu () * * @param string $ items The HTML list content for the menu items. * @param stdClass $ args An object containing wp_nav_menu () arguments. * / $ items = apply_filters ("wp_nav_menu_items", $ items, $ args); / ** * Filters the HTML list content for a specific navigation menu. * * @since 3.0.0 * * @see wp_nav_menu () * * @param string $ items The HTML list content for the menu items. * @param stdClass $ args An object containing wp_nav_menu () arguments. * / $ items = apply_filters ("wp_nav_menu _ ($ menu-> slug) _items", $ items, $ args); // Don "t print any markup if there are no items at this point. If (empty ($ items)) (return false;) $ nav_menu. = Sprintf ($ args-> items_wrap, esc_attr ($ wrap_id), esc_attr ( $ wrap_class), $ items); unset ($ items); if ($ show_container) ($ nav_menu. = "container. ">"; ) / ** * Filters the HTML content for navigation menus. * * @since 3.0.0 * * @see wp_nav_menu () * * @param string $ nav_menu The HTML content for the navigation menu. * @param stdClass $ args An object containing wp_nav_menu () arguments. * / $ nav_menu = apply_filters ("wp_nav_menu", $ nav_menu, $ args); if ($ args-> echo) (echo $ nav_menu;) else (return $ nav_menu;))