Wordpress Custom Walker:当前菜单项

Wordpress Custom Walker: Current Menu Item

我是 wordpress 的新手,正在尝试构建一个自制的菜单结构。我找到了一个定制助行器并根据我的需要对其进行了定制。菜单工作正常,现在您可以在 my site.

上看到

它通过 <?php wp_nav_menu(array('walker' => new Custom_Nav_Walker())); ?> 加载到 header.php 中。该命令嵌套在 ul 标签中,因此 walker 仅在其中生成 li 标签。

现在我想添加一个名为 active 的 class 到当前父菜单项及其下拉子菜单项。我在网上搜索了几个小时,但没有找到任何我能理解的东西……

_

任何人都可以帮助我将 class 应用到我的自定义助行器中吗?

提前致谢!

哈罗

class Custom_Nav_Walker extends Walker_Nav_Menu {
  function start_lvl(&$output, $depth = 0, $args = array()) {
      $output .= "";
  }

  function end_lvl(&$output, $depth = 0, $args = array()) {
    $output .= "";
  }

  function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {

    parent::start_el($item_html, $item, $depth, $args);

    if ($item->is_dropdown && ($depth === 0)) {
      $output .= "";

    } elseif ($depth === 0) {
      $output .= "<li class=\"navbar-list-item\"><div class=\"w-dropdown\" data-ix=\"dropdown\"><div class=\"w-dropdown-toggle navbar-link\"><h5>".esc_attr($item->title)."</h5></div><nav class=\"w-dropdown-list dropdown-list\">";

    } elseif ($depth > 0) {
      $output .= "<a class=\"w-dropdown-link dropdown-link\" href=\"".esc_attr($item->url)."\">".esc_attr($item->title)."";
    }
  }

  function end_el(&$output, $item, $depth=0, $args=array()) {  

        if ($item->is_dropdown && ($depth === 0)) {
      $output .= "";

    } elseif ($depth === 0) {
$output .= "</nav></div></li>";

    } elseif ($depth > 0) {
    $output .= "</a>";
    }
  }  
}

我写了一个新的自定义助行器。这个现在有效。也许有人最终会在某个时候遇到同样的问题。所以这是解决方案:

    class Custom_Nav_Walker extends Walker_Nav_Menu {
  function start_lvl(&$output, $depth = 0, $args = array()) {
      $output .= "";
  }

  function end_lvl(&$output, $depth = 0, $args = array()) {
    $output .= "";
  }

  function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {


      $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );






    parent::start_el($item_html, $item, $depth, $args);

    if ($item->is_dropdown && ($depth === 0)) {
      $output .= "";

    } elseif ($depth === 0) {
      $output .= "<li class=\"navbar-list-item\"><div class=\"w-dropdown\" data-ix=\"dropdown\"><div class=\"w-dropdown-toggle navbar-link ".esc_attr($class_names)."\"><h5>".esc_attr($item->title)."</h5></div><nav class=\"w-dropdown-list dropdown-list\">";

    } elseif ($depth > 0) {
      $output .= "<a class=\"w-dropdown-link dropdown-link ".esc_attr($class_names)."\" href=\"".esc_attr($item->url)."\">".esc_attr($item->title)."";
    }
  }

  function end_el(&$output, $item, $depth=0, $args=array()) {  

        if ($item->is_dropdown && ($depth === 0)) {
      $output .= "";

    } elseif ($depth === 0) {
$output .= "</nav></div></li>";

    } elseif ($depth > 0) {
    $output .= "</a>";
    }
  }  
}