dongyou2279 2013-10-31 10:32
浏览 83
已采纳

更改wordpress中的子菜单类

When using wp_nav_menu in my theme, I want to change Worpdress' default sub-menu class for items that contain a child list (to dropdown to fit for the Foundation framework).

I have reviewed this post on the topic but I cannot seem to get it to function correctly.

In my functions.php file I have inserted:

class My_Sub_Menu extends Walker_Nav_Menu {
  function start_lvl(&$output, $depth) {
    $indent = str_repeat("\t", $depth);
    $output .= "
$indent<ul class=\"dropdown\">
";
  }
}

And in my header.php file I have:

<?php
    $defaults = array(
        'theme_location'  => 'header-nav',
        'menu_class'      => 'right',
        'walker'          => new My_Sub_Menu(),
        'container'       =>  false
    );
    wp_nav_menu( $defaults );
?>

But nothing occurs. Am I misunderstanding where to insert the code?

  • 写回答

1条回答 默认 最新

  • du8791069 2013-10-31 11:33
    关注

    Is it because you haven't specified an end_lvl for your class My_Sub_Menu extends Walker_Nav_Menu?

    class My_Sub_Menu extends Walker_Nav_Menu {
      function start_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "
    $indent<ul class=\"dropdown\">
    ";
      }
      function end_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>
    ";
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?