我的wordpress walker代码有一些问题。我知道可以通过“管理”菜单部分手动添加自定义css类。但我试图实现的是,每当客户端添加子菜单页时,自动完成此操作。
我采取的第一步是替换ul-li导航,并将其更改为a href导航样式。这是可行的。现在,我正在寻找一种方法来添加一个类到子菜单a href items。。。
这就是我的步行者现在的样子。
class Description_Walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
$classes = empty($item->classes) ? array () : (array) $item->classes;
$class_names = join(\' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) );
!empty ( $class_names ) and $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';
$output .= "";
$attributes = \'\';
!empty( $item->attr_title ) and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';
!empty( $item->target ) and $attributes .= \' target="\' . esc_attr( $item->target ) .\'"\';
!empty( $item->xfn ) and $attributes .= \' rel="\' . esc_attr( $item->xfn ) .\'"\';
!empty( $item->url ) and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
$item_output = $args->before
. ""
. $args->link_before
. $title
. \'\'
. $args->link_after
. $args->after;
if (array_search(\'menu-item-has-children\', $item->classes)) {
$output .= sprintf("\\n<div data-delay=\'0\' class=\'w-dropdown\'><div class=\'dropdown-toggle\'><div class=\'icon icon-dropdown-toggle\'></div><div>$title</div></div>
\\n", ( array_search(\'current-menu-item\', $item->classes) || array_search(\'current-page-parent\', $item->classes) ) ? \'active\' : \'\', $item->url,
$item->title);
} else {
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
function start_lvl(&$output, $depth, $args ) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n$indent<nav class=\\"dropdown-list\\">\\n<div class=\\"submenu_speech\\"></div>";
}
function end_lvl( &$output, $depth, $args = array() ) {
$indent = str_repeat("\\t", $depth);
$output .= "$indent</nav></div>\\n";
}
function end_el(&$output, $item, $depth, $args) {
$output .= "\\n";
}
}
SO网友:Danny
class Description_Walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
$classes = empty($item->classes) ? array () : (array) $item->classes;
if( $depth == 1 ){
$class_names = join(\' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) );
!empty ( $class_names ) and $class_names = \' class="dropdown-link w-dropdown-link \'. esc_attr( $class_names ) . \'"\';
} else {
$class_names = join(\' \', apply_filters( \'nav_menu_css_class\', array_filter( $classes ), $item ) );
!empty ( $class_names ) and $class_names = \' class="\'. esc_attr( $class_names ) . \'"\';
}
$output .= "";
$attributes = \'\';
!empty( $item->attr_title ) and $attributes .= \' title="\' . esc_attr( $item->attr_title ) .\'"\';
!empty( $item->target ) and $attributes .= \' target="\' . esc_attr( $item->target ) .\'"\';
!empty( $item->xfn ) and $attributes .= \' rel="\' . esc_attr( $item->xfn ) .\'"\';
!empty( $item->url ) and $attributes .= \' href="\' . esc_attr( $item->url ) .\'"\';
$title = apply_filters( \'the_title\', $item->title, $item->ID );
$item_output = $args->before
. "<a $attributes $class_names>"
. $args->link_before
. $title
. \'</a>\'
. $args->link_after
. $args->after;
if (array_search(\'menu-item-has-children\', $item->classes)) {
$output .= sprintf("\\n<div data-delay=\'0\' class=\'w-dropdown\'><div class=\'dropdown-toggle w-dropdown-toggle\'><div class=\'icon w-icon-dropdown-toggle\'></div><div>$title</div></div>
\\n", ( array_search(\'current-menu-item\', $item->classes) || array_search(\'current-page-parent\', $item->classes) ) ? \'active\' : \'\', $item->url,
$item->title);
} else {
$output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
}
function start_lvl(&$output, $depth, $args ) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n$indent<nav class=\\"dropdown-list w-dropdown-list\\">\\n<div class=\\"submenu_speech\\"></div>";
}
function end_lvl( &$output, $depth, $args = array() ) {
$indent = str_repeat("\\t", $depth);
$output .= "$indent</nav></div>\\n";
}
function end_el(&$output, $item, $depth, $args) {
$output .= "\\n";
}
}
因此,当$depth==1时,将“dropdown link w-dropdown-link”类添加到a标记中。
这是可行的,但这是合法的解决方案吗?