Delete unnecessary WordPress navigation menus and Class ID

Use  wordpress station program ultimately used menu functions official, for the love of a toss of the preceding WEB lovers that uncontrollable css selectors really looked headache, as this use the following html menu function preceding the default code generated Code which appeared in more than id and class selectors, but these are all we can to a certain ratio, and thus a series of redundant code.

Speaking before deleting superfluous WordPress navigation menu ID and Class, we first look at increasing the WordPress menu template method, in functions.php file with the following code:

 
  1. if(function_exists('register_nav_menus')){
  2. register_nav_menus(
  3. array(
  4. 'header-MENU' => __ ( 'menu name A' ) ,
  5. 'footer-MENU' => __ ( 'menu name B' ) ,
  6. 'Sider-MENU' => __ ( 'menu name C' )
  7. )
  8. );
  9. }

The easiest way to call the front desk to add the following codes:

  1. <?php wp_nav_menu(); ?>

It may be performed more control on this menu, as follows:

  1. <?php wp_nav_menu(
  2. array(
  3. 'theme_location'   => '' // name specified navigation display, if not set, a first display
  4. 'menu'            => 'header-menu',
  5. 'Container'        => 'NAV' , // outermost container tag name
  6. 'container_class' => 'Primary' , // class name outermost container
  7. 'container_id is'     => '' , // outermost container id value
  8. 'menu_class'      => 'sf-menu', //ul标签class
  9. 'menu_id'          => 'the topnav' , // UL tag id
  10. 'echo'             => to true , // whether the printing, the default is true, if you want to use the navigation code an assignment may be set to false
  11. 'fallback_cb'      => 'wp_page_menu' , // backup navigation menu functions not called in the background to set the navigation
  12. 'before'           => '' , // display a navigation tag before
  13. 'After'            => '' , // the navigation display after a tag
  14. 'link_before'      => '' , // name displayed after the navigation links
  15. 'link_after'       => '' , // name displayed before the navigation links
  16. 'items_wrap'      => '<ul id="%1$s">%3$s</ul>',
  17. 'depth'            => 0 , the menu display //// layers, the default is to display all layers 0,0
  18. 'Walker'           => '' // // call the object definition display a navigation menu));
  19. ?>

Wp_nav_menu using () adding menu, many CSS selector outputs, such as the following code:

  1. <li id="menu-item-6" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-6"><a href="/">WPMAKER</a></li>
  2. <li id="menu-item-13" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-13"><a href="/news">WordPress资讯</a></li>
  3. <li id="menu-item-8" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-8"><a href="/themes">WordPress主题</a></li>

The above code is generated with the default menu functions which I only used the menu-item like id = "menu-item-13" class = "menu-item menu-item-type-taxonomy menu-item-object-category menu -item-13 "these are useless, and today I put the code to remove the excess css selected to share to you, the code is very simple to remove these wordpress code does not use a filter, you can add the following code to the topic the functions.php, removing all css selectors:

  1. /**
  2.  * Removed redundant menu CSS selectors
  3.  */
  4. add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
  5. add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
  6. add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
  7. function my_css_attributes_filter($var) {
  8. 	return is_array($var) ? array() : '';
  9. }

Output results are as follows, streamlined enough of it:

  1. <li><a href="/">WPMAKER</a></li>
  2. <li><a href="/news">WordPress资讯</a></li>
  3. <li><a href="/themes">WordPress主题</a></li>

However, you may want to consider the current menu to define certain styles, such as highlight the current menu, then you need to keep some CSS selectors to define the current menu. Common current menu selector has the following four:

  • current-post-ancestor
  • current-menu-ancestor
  • current-menu-item
  • current-menu-parent
  1. /**
  2.  * Removed redundant menu CSS selectors
  3.  */
  4. add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
  5. add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
  6. add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
  7. function my_css_attributes_filter($var) {
  8. 	return is_array($var) ? array_intersect($var, array('current-menu-item','current-post-ancestor','current-menu-ancestor','current-menu-parent')) : '';
  9. }

As a result, the navigation menu in WordPress we often use the selectors have (menu-item, current-menu-item, current-post-parent, current-menu-parent) these so we can put a few labels are retained if the current access is the code WPMAKER home page, the output is:

  1. <li class="current-menu-item"><a href="/">WPMAKER</a></li>
  2. <li><a href="/news">WordPress资讯</a></li>
  3. <li><a href="/themes">WordPress主题</a></li>

Retains class = "current-menu-item", add style to this class.

Guess you like

Origin www.cnblogs.com/app4561/p/11788258.html