Walker gives additional taxonomy name?

I’m a beginner at WordPress, so I would appreciate it if any of you could nudge me in the right way. Anyway, what irks me is how wp_list_categories (and similar functions) produce a lot of unnecessary classes and stuff I don’t need.

I tried creating my own walker class based on Walker_Category, and I seem to get it working. All I need as a class is that a “current” class appears when it is the current category, but when it is not, I don’t want any classes at all to show up.

Read More

However, for some reason I’m getting an additional class by my taxonomy name. I’ve looked over my walker class a gazillion times but can’t seem to be seeing where it is indicated that the taxonomy name should be stored in $class. I also looked in the original Walker class, but haven’t gotten much out of it.

Please help? 🙂

 class Meow_Walker extends Walker_Category {

    var $tree_type = 'category';
    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("t", $depth);
        $output .= "$indent<ul class='children'>n";
    }
    function end_lvl( &$output, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $indent = str_repeat("t", $depth);
        $output .= "$indent</ul>n";
    }

    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        extract($args);

        $cat_name = esc_attr( $category->name );
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $link = '<a href="' . esc_url( get_term_link($category) ) . '"';
        $link .= '>';
        $link .= $cat_name . '</a>';

        if ( !empty($feed_image) || !empty($feed) ) {
            $link .= ' ';

            if ( empty($feed_image) )
                $link .= '(';

            $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';

            $link .= '>';

            if ( empty($feed_image) )
                $link .= $name;
            else
                $link .= "<img src='$feed_image'$alt$title" . ' />';

            $link .= '</a>';

            if ( empty($feed_image) )
                $link .= ')';
        }

        if ( !empty($show_count) )
            $link .= ' (' . intval($category->count) . ')';

        if ( 'list' == $args['style'] ) {
            $output .= "t<li";
            if ( !empty($current_category) ) {
                $_current_category = get_term( $current_category, $category->taxonomy );
                if ( $category->term_id == $current_category )
                    $class .=  'current';
                elseif ( $category->term_id == $_current_category->parent )
                    $class .=  'current-parent';
            }
            $output .=  ' class="' . $class . '"';
            $output .= ">$linkn";
        } else {
            $output .= "t$link<br />n";
        }
    }

    function end_el( &$output, $page, $depth = 0, $args = array() ) {
        if ( 'list' != $args['style'] )
            return;

        $output .= "</li>n";
    }

}

Edit: I found a workaround, but I’d still like to know where $class gets its taxonomy name from…

Related posts

Leave a Reply

1 comment

  1. Edit: After looking at the wp_list_categories() code I noticed that you can just pass a class parameter to the function. If you pass an empty string, the built-in Walker will always echo class="". To get rid of the class attribute entirely, you will still have to use a custom walker and edit start_el:

    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        // ...
        if ( 'list' == $args['style'] ) {
            $output .= "t<li";
            if ( !empty($current_category) ) {
                $_current_category = get_term( $current_category, $category->taxonomy );
                if ( $category->term_id == $current_category )
                    $class =  'current'; // removed string operator. not needed here anymore.
                elseif ( $category->term_id == $_current_category->parent )
                    $class =  'current-parent'; // see above
            }
            if ( $class ) // skip output if no class is set
                $output .=  ' class="' . $class . '"';
            $output .= ">$linkn";
        } else {
            $output .= "t$link<br />n";
        }
        // ...
    }
    

    Whenever WordPress runs the callback for start_el it will pass the taxonomy name (plus the default class) as well as other information as 4th parameter: $args.

    To remove the default class you could unset/delete the class element in the $args before the extract() line:

    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        unset( $args['class'] );
        extract($args);
        // ...   
    }
    

    You would also want to change the code where the walker sets $class to remove the string operator.

    if ( $category->term_id == $current_category )
        $class = 'current';
    elseif ( $category->term_id == $_current_category->parent )
        $class = 'current-parent';