数据属性未显示在Wordpress菜单中

时间:2018-10-26 21:49:38

标签: wordpress

我已经从Walker类创建了一个函数,我想在子菜单中添加新属性。我已添加了这些内容,但未显示在屏幕上。

class function_group_nav_walker extends Walker_Nav_Menu { 

 function start_lvl( &$output, $depth = 0, $args = array() ) {
    // 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 for output.
    $output .= "\n" . $indent . '<ul class="' . $class_names . '" data-slick-index="-1" data-hover="dropdown" data-animations="zoomIn zoomIn zoomIn zoomIn">' . "\n";
}

 }

此外,data-slick-index的范围应为-1至1000,每个子菜单各不相同

array( 
 'theme_location' => 'menu-2', 
  'container'=>'ul',  
  'menu_class' => 'clearfix',
  'fallback_cb'  => '',
   'walker' => $function_group_walker,
   'echo' => false
   )  

1 个答案:

答案 0 :(得分:0)

在活动主题的function.php中添加以下代码

add_filter( 'nav_menu_link_attributes', 'wpse121123_contact_menu_atts', 10, 3 );
function wpse121123_contact_menu_atts( $atts, $item, $args )
{
  // The ID of the target menu item
  $menu_target = 123;

  // inspect $item
  if ($item->ID == $menu_target) {
    $atts['data-slick-index'] = -1;
  }
  return $atts;
}