【问题标题】:Wordpress Custom Walker: Current Menu ItemWordpress Custom Walker:当前菜单项
【发布时间】:2015-07-15 23:41:51
【问题描述】:

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

它通过<?php wp_nav_menu(array('walker' => new Custom_Nav_Walker())); ?> 加载到 header.php 中。该命令嵌套在一个ul标签内,所以walker只生成里面的li标签。

现在我想向当前父菜单项添加一个名为active 的类,它是下拉子项。我在网上搜索了几个小时,但没有找到任何我能理解的内容……

_

谁能帮我将该类实现到我的自定义walker中?

提前致谢!

贾罗

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>";
    }
  }  
}

【问题讨论】:

    标签: wordpress class menu


    【解决方案1】:

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

        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>";
        }
      }  
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 2021-11-06
      相关资源
      最近更新 更多