Adding ‘active’ class to custom taxonomies using walker in functions.php

I’m trying to use the following code in functions.php in order to show a menu with categories (custom taxonomies actually). I simply want it to show active when on the current page. What am I doing wrong?

I also see that the $category = get_category($mycatid); line doesn’t populate the $category variable.

<?php
class walker_archives extends Walker_Nav_Menu
{
    public function start_el( &$output, $item, $depth, $args )
    {
        $attributes  = '';

        ! empty ( $item->attr_title )
            and $item->attr_title !== $item->title
            and $attributes .= ' title="' . esc_attr( $item->attr_title ) .'"';

        ! empty ( $item->url )
            and $attributes .= 'href="' . esc_attr( $item->url ) .'"';

        $attributes  = trim( $attributes );
        $title       = apply_filters( 'the_title', $item->title, $item->ID );
        $item_output = "$args->before<a $attributes>$args->link_before$title</a>"
                        . "$args->link_after$args->after";

        $mycatid = $item->object_id;
        $category = get_category($mycatid);
        $mycatslug = $category->slug;
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names .= in_array("current_page_item",$item->classes) ? ' active' : '';
        $class_names = ' class="staysexy'. esc_attr( $class_names );
        $output .= $indent . '
                    <li id="nav-menu-item-'. $item->ID . '" class="genre-'. $mycatslug . '">';
        $output .= apply_filters(
            'walker_nav_menu_start_el'
            ,   $item_output
            ,   $item
            ,   $depth
            ,   $args
        );
    }
    public function start_lvl( &$output )
    {
        $output .= '<ul class="sub-menu">';
    }
    public function end_lvl( &$output )
    {
        $output .= '</ul>';
    }
    function end_el( &$output )
    {
        $output .= '</li>';
    }
}?>

Related posts

Leave a Reply

1 comment