0

I'm building a blog using a custom walker for my submenu which looks like this:

class Submenu_Walker extends Walker_Nav_Menu {
    function start_el ( &$output, $item, $depth = 0, $args = array(), $id = 0 )
    {
        $output .= sprintf('
            <li>
                <a href="%s">
                    <span class="title">%s</span>
                    <h2>%s</h2>
        ', get_permalink($item->ID), $item->post_title, $item->post_excerpt);
    }

    function end_el( &$output, $item, $depth = 0, $args = array() )
    {
        $output .= sprintf('
                </a>
            </li>
        ');
    }
}

When navigated to the page I would like to add the WordPress active class .current-menu-item to it. I've tried adding the default classes from Walker_Nav_Menu like following:

$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 ) );
$class_names = ' class="' . esc_attr( $class_names ) . '"';

But this didn't worked as aspected, only the class menu-item-ID was added. Someone who can help me out with this?

Thanks for the help.

Edit after comment:

$submenu = wp_list_pages( array(
  'child_of' => $post->post_parent : $post->ID,
  'title_li' => '',
  'echo' => false,
  'walker' => new Submenu_Walker
) );

Above is how I list the pages. After that I'll simply echo $submenu. In functions.php I'm including the Walker with: include_once('inc/walkers/SubmenuWalker.php');

  • That code should work. Can you show us how exactly have you implemented it? – cybmeta Mar 5 '15 at 16:38
  • I've edited my post with more specifications. Hopefully this can help out. As I said, a class is added, but not the correct one(s). – ronnyrr Mar 5 '15 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.