Pregunta

Tengo el siguiente menú en mi encabezado:

<?php
$args = array(
    'menu'            => 'Main Menu',
    'container'       => false,
    'depth'           => 1,
    'items_wrap'      => '%3$s',
    'walker'          => new Bar_List_Walker_Nav_Menu
);
wp_nav_menu($args);
?>

Y quiero que la salida se vea así:

link1 | link2 | link3 | link4 | link5

Así que me propuse hacer una función de Walker aquí es donde llegué:

class Bar_List_Walker_Nav_Menu extends Walker_Nav_Menu {
    public $count;
    function start_lvl(&$output, $depth) {}
    function end_lvl(&$output, $depth) {}
    function start_el(&$output, $item, $depth, $args) {
        $attributes = ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';

        $item_output .= '<a'. $attributes .'>';
        $item_output .= apply_filters( 'the_title', $item->title, $item->ID );
        $item_output .= '</a>';

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
    function end_el(&$output, $item, $depth) {
        static $count;
        $count++;
        if(!$this->count >= $count) {
            $output .= " | ";
        }
    }
    function walk( $elements, $max_depth ) {
        $this->count = count($elements);
        parent::walk( $elements, $max_depth );
    }
}

Esto genera el siguiente error:

Warning: Missing argument 4 for Bar_List_Walker_Nav_Menu::start_el() in C:\xampp\DEV\Stace\trunk\wp-content\themes\philosophy\functions.php on line 100

Si elimino la función walk() De mi clase de Walker funciona bien, excepto que el recuento ya no se agarra y, como resultado, uno | Demasiados se agrega al final de mi navegación.

¿Alguien puede trabajar el código para llegar a mi salida deseada?

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
scroll top