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.
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…
Edit: After looking at the
wp_list_categories()
code I noticed that you can just pass aclass
parameter to the function. If you pass an empty string, the built-in Walker will always echoclass=""
. To get rid of theclass
attribute entirely, you will still have to use a custom walker and editstart_el
: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 theextract()
line:You would also want to change the code where the walker sets
$class
to remove the string operator.