我目前在 WordPress 中为我的 WP 菜单使用自定义助行器,但我只需要最后一次修改。我需要知道如何...
为所有“子菜单”ul 中的所有奇数 li 赋予一个“奇数”类。所以这是父级之后的第一个“子菜单”,所有“子菜单”都在其中。
这是我目前正在使用的自定义助行器,所以如果您可以编辑它,那就太棒了!
class Menu_With_Description extends Walker_Nav_Menu
{
function start_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu level-".$depth."\">\n";
}
function end_lvl(&$output, $depth) {
$indent = str_repeat("\t", $depth);
$output .= "$indent<div class=\"fix\"></div></ul><div class=\"fix\"></div>\n";
}
/**
* Start the element output.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. May be used for padding.
* @param array $args Additional strings.
* @return void
*/
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output )
{
$id_field = $this->db_fields['id'];
if ( is_object( $args[0] ) ) {
$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
}
return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
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
)
);
if ($args->has_children && $depth == 0){
! empty ( $class_names )
and $class_names = ' class="'. esc_attr( $class_names ) . ' has_children"';
}else{
! empty ( $class_names )
and $class_names = ' class="'. esc_attr( $class_names ) . '"';
}
$output .= "<li id='menu-item-$item->ID' $class_names>" ;
$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 ) .'"';
// insert description for top level elements only
// you may change this
$description = ( ! empty ( $item->description ) and 0 == $depth )
? '<span class="description">' . esc_attr( $item->description ) . '</span>' : '';
$title = apply_filters( 'the_title', $item->title, $item->ID );
if ( $depth == 0 ) {//top level items
$item_output = $args->before
."<div class='parent'><div class='cat-icon'></div><div class='title-desc'>"
. "<a $attributes>"
. $args->link_before
. $title
. '</a><br>'
. $args->link_after
. $description
. '</div></div>'
. $args->after;
}else{//everything else
$item_output = $args->before
. "<a $attributes>"
. $args->link_before
. $title
. '</a> '
. $args->link_after
. $args->after;
}
// Since $output is called by reference we don't need to return anything.
$output .= apply_filters(
'walker_nav_menu_start_el'
, $item_output
, $item
, $depth
, $args
);
}
}