Skip to content

Instantly share code, notes, and snippets.

@jesgs
Last active December 11, 2015 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jesgs/4626596 to your computer and use it in GitHub Desktop.
Save jesgs/4626596 to your computer and use it in GitHub Desktop.
Add checkboxes to categories using a Walker class and wp_list_categories
<?php
/**
* My Project
*
* @package MyProject
* @subpackage JesGs_Walker_Category
* @author Jess Green <jgreen@psy-dreamer.com>
* @version $Id$
*/
/**
* JesGs_Walker_Category
*
* @todo move contents of start_el method to separate template.
*
* @package JesGs_Walker_Category
* @author Jess Green <jgreen@psy-dreamer.com>
*/
class JesGs_Walker_Category extends Walker_Category
{
/**
* @see Walker_Category::start_el
*/
public function start_el(&$output, $category, $depth, $args, $current_object_id = 0)
{
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
$cat_slug = esc_attr( $category->slug );
if ( 'list' == $args['style'] ) {
$output .= "\t<li";
$class = 'cat-item cat-item-' . $category->term_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= ' current-cat';
elseif ( $category->term_id == $_current_category->parent )
$class .= ' current-cat-parent';
}
$output .= ' class="' . $class . '"';
$output .= "><input type=\"checkbox\" name=\"$cat_slug\" />&nbsp;$cat_name";
} else {
$output .= "\t$cat_name<br />\n";
}
}
}
@twoelevenjay
Copy link

Perfect! Thank you so much.

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