WordPress教程

WordPress使用wp_nav_menu函数生成需要的class结构

阿里云

WordPress 制作导航菜单,会使用 wp_nav_menu 函数,它可以自动调用出后台创建的导航菜单。但是使用默认的 wp_nav_menu 函数生成的菜单结构比较单调,有时很难达到我们自己的需要。下面介绍一下 WordPress 如何使用 wp_nav_menu 函数生成需要的 class 结构。

第一步:将下的函数代码粘贴到自己模板的 Functions.php 中;

也想出现在这里?联系我们
创客主机
  1. *自定义二级菜单导航开始*/
  2. class wp_bootstrap_navwalker extends Walker_Nav_Menu {public function start_lvl( &$output, $depth = 0, $args = array() ) {
  3. $indent = str_repeat( "\t", $depth );
  4.  
  5. $output .= $indent.'<ul class="dropdown-menu">';//00001-下拉UL样式
  6. }
  7.  
  8. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  9. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  10.  
  11. //判断标题文字是不是指定文字,然后显示样式
  12. if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
  13. $output .= $indent . '<li role="presentation" class="divider">';
  14. } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
  15. $output .= $indent . '<li role="presentation" class="divider">';
  16. } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
  17. $output .= $indent . '<li role="presentation" class="dropdown-header">' . esc_attr( $item->title );
  18. } else if ( strcasecmp($item->attr_title, 'disabled' ) == 0 ) {
  19. $output .= $indent . '<li role="presentation" class="disabled"><a href="#">' . esc_attr( $item->title ) . '</a>';
  20.  
  21. //判断结束
  22.  
  23. } else {
  24.  
  25. $class_names = $value = '';
  26.  
  27. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  28. $classes[] = 'menu-item-' . $item->ID;
  29.  
  30. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  31.  
  32. if ( $args->has_children )
  33. $class_names .= ' dropdown nav1';//00002-有下拉菜单的父LI的样式
  34.  
  35. if ( !$args->has_children && $depth === 0 )
  36. $class_names .= ' nav1';//00003-没有下拉菜单的父LI的样式
  37.  
  38. if ( in_array( 'current-menu-item', $classes ) )
  39. $class_names .= ' active';//00004-当前样式
  40.  
  41. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  42.  
  43. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );//00005-菜单所有li的添加ID
  44. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  45.  
  46. $output .= $indent . '<li' . $id . $value . $class_names .'>';
  47.  
  48. $atts = array();
  49. $atts['title'] = ! empty( $item->title ) ? $item->title : '';
  50. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  51. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  52.  
  53. // If item has_children add atts to a.
  54. if ( $args->has_children && $depth === 0 ) {
  55. $atts['href'] = $item->url;
  56. $atts['data-toggle'] = 'dropdown';
  57. $atts['class'] = 'alink';//00006-有下拉的第一个父A的CLASS
  58. $atts['aria-haspopup'] = 'true';//控制父A链接是否可点击
  59. } else {
  60. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  61. }
  62.  
  63. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  64.  
  65. $attributes = '';
  66. foreach ( $atts as $attr => $value ) {
  67. if ( ! empty( $value ) ) {
  68. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  69. $attributes .= ' ' . $attr . '="' . $value . '"';
  70. }
  71. }
  72.  
  73. $item_output = $args->before;
  74.  
  75. if ( $args->has_children && 0 === $depth )
  76.  
  77. $item_output .= '<a data-toggle="dropdown"'. $attributes .'>';//00007-有下拉的第一个父A里面加内容
  78.  
  79. elseif ( ! empty( $item->attr_title ) )
  80. $item_output .= '<a'. $attributes .'><span class="glyphicon ' . esc_attr( $item->attr_title ) . '"></span>&nbsp;';
  81.  
  82. else
  83. $item_output .= '<a'. $attributes .'>';
  84.  
  85. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  86. $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';////00008-有下拉的第一个父A后面加span,也可以加到外面
  87. $item_output .= $args->after;
  88.  
  89. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  90. }
  91. }
  92.  
  93. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  94. if ( ! $element )
  95. return;
  96.  
  97. $id_field = $this->db_fields['id'];
  98.  
  99. // Display this element.
  100. if ( is_object( $args[0] ) )
  101. $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
  102.  
  103. parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
  104. }
  105. }

第二步:使用以下的代码来调用菜单

  1. <?php
  2. wp_nav_menu( array(
  3. 'theme_location' => 'topmenu',
  4. 'depth' => 2,
  5. 'container' => false,
  6. 'menu_class' => 'nav navbar-nav navlist',
  7. 'menu_id' => 'topmeau',
  8. 'fallback_cb' => 'wp_page_menu',
  9. //添加或更改walker参数
  10. 'walker' => new wp_bootstrap_navwalker())
  11. );
  12. ?>

如果想修改 class 类,可以在第一步的代码中修改,达到自己需要的 class 结构。

WordPress 使用 wp_nav_menu 函数生成需要的 class 结构

已有 196 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!