Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>';
    }
}?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Changing current_page_item to current-menu-item solved the problem because these were taxonomies and not pages.

Whats the difference between current_page_item and current-menu-item

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.