Hallo da draußen,
ich versuche in eine Walkerfunktion einen Counter für die Listenelemente einzubauen. Ich hab schon einiges probiert, aber funktioniert, so dass jedes Listenelement der obersten Ebene eine numierierte ID in der Reihenfolge bekommt, hat es nicht. BIn für jede Hilfe dankbar.
Dies ist meine Walkerfunktion:
class WPSE_78121_Sublevel_Walker extends Walker_Nav_Menu
{
function start_el ( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
// Most of this code is copied from original Walker_Nav_Menu
global $wp_query, $wpdb;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$x++;
$class_names = $value = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = '';
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = ' class="menu-item"';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = ' id=""';
$has_children = $wpdb->get_var("SELECT COUNT(meta_id)
FROM wp_postmeta
WHERE meta_key='_menu_item_menu_item_parent'
AND meta_value='".$item->ID."'");
$output .= $indent . '<li' . $value . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
if ( $depth == 0 ) {
$attributes .= ' id=""';
}
// Check if menu item is in main menu
if ( $depth == 0 && $has_children > 0 ) {
$item_html = '';
// These lines adds your custom class and attribute
$attributes .= ' class="trig"';
$attributes .= ! empty( $item->url ) ? ' href="#"' : '';
} else {
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
// Add the caret if menu level is 0
if ( $depth == 0 && $has_children > 0 ) {
$item_output .= '';
}
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<div id='' class='p7dmm-sub-wrapper'><ul class='sub-menu'>\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul></div>\n";
}
}
Alles anzeigen