Skip to content

Instantly share code, notes, and snippets.

@tenman
Created May 9, 2012 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tenman/2644185 to your computer and use it in GitHub Desktop.
Save tenman/2644185 to your computer and use it in GitHub Desktop.
Add classes wp_list_categories
//themes functions.php
class MyWalker extends Walker_Category {
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = esc_attr( $category->name );
$link = '<a href="%1$s" rel="%2$s">%3$s</a>';
$link = sprintf($link
, esc_attr( get_term_link($category) )
, $category->slug
, $cat_name
);
$check_mago = count( get_ancestors( $category->term_id, 'category' ) );
if($check_mago == 2 and $category->category_parent > 0 ){
$class = 'mago';
}
if($check_mago == 1 and $category->category_parent > 0 ){
$class = 'kodomo';
}
if($category->category_parent == 0){
$class = 'oya';
}
$output .= '<li class="'.$class.'">'.$link;
}
}
//template
<?php
$MyWalker = new MyWalker();
$args = array('walker' => $MyWalker);
?>
<ul>
<?php
wp_list_categories($args);
?>
</ul>
@tarikcayir
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment