Forum Moderators: phranque
Basically, I'm building a custom theme and finding that by default WordPress spits out lots of bloated code for the Categories list. Specifically, by default, the categories template tag
wp_list_categories() creates markup with list items for that category that each include CSS class names which I don't need - BUT I can't find how to stop them from being generated. Here's a quick example:
<ul id="categories">
<?php
wp_list_categories('title_li=');?>
</ul>
This then generates (generically) the following markup:
<ul id="categories">
<li class="cat-item cat-item-1"><a href="#">Category name</a></li>
<li class="cat-item cat-item-2"><a href="#">Category name</a></li>
...
</ul>
As shown, I am using an ID on the containing UL, and therefore for my purposes I don't need those class attributes on the individual LIs. But WP just puts them in there anyway. This is not controlled by any Theme files, and I am searching through the widget.php template file for where this code bloat is generated but no luck. Any ideas where the Category list items' bloated markup / class attributes are coming from?
<ul id="categories">
<li><a href="#">Category name</a></li>
<li><a href="#">Category name</a></li>
...
</ul>
I'm nitpicking here, but I like my code clean and without bloat... it should be that if you need class or any attributes, the wp_list_categories tag will allow you to pass it arguments to get what you need in the output. Otherwise, it should just keep it to the minimum markup... IMHO.
Still looking for the suspect include template file that controls this...
The relevant code is in classes.php (under wp-includes).
The original code, starting at line 635:
if ( 'list' == $args['style'] ) {
$output .= "\t<li";
$class = 'cat-item cat-item-'.$category->term_id;
if ( $current_category && ($category->term_id == $current_category) )
$class .= ' current-cat';
elseif ( $_current_category && ($category->term_id == $_current_category->parent) )
$class .= ' current-cat-parent';
$output .= ' class="'.$class.'"';
$output .= ">$link\n";
} else {
$output .= "\t$link<br />\n";
}
return $output;
}
Updated to following:
if ( 'list' == $args['style'] ) {
$output .= "\t<li";
if (!( $current_category && ($category->term_id == $current_category) ) ¦¦!( $_current_category && ($category->term_id == $_current_category->parent) ) )
$output .= ">$link\n";
return $output;
} else {
if ( $current_category && ($category->term_id == $current_category) )
$class .= ' current-cat';
elseif ( $_current_category && ($category->term_id == $_current_category->parent) )
$class .= ' current-cat-parent';
$output .= ' class="'.$class.'"';
$output .= ">$link\n";
}
return $output;
}
The end result being that by default, the list items are now generated WITHOUT the unnecessary class attributes. If you are on a Category page, then the 'selected' class attribute will be generated for that category's respective list item.
I have tested on the index page and the markup is good. I'm still building the rest of the theme templates so I have tested whether those class attributes actually *WILL* show up on the category pages... TBD.
[edited by: phranque at 3:35 pm (utc) on Jan. 22, 2008]