Return to Snippet

Revision: 20929
at December 1, 2009 12:12 by gfazioli


Initial Code
/**
 * Restituisce una lista (UL/LI) delle categorie
 * @params $selected_cats	 Array delle categorie selezionate
 *
 * @return HTML
 */
function get_categories_checkboxes( $selected_cats = null ) {
	$all_categories = get_categories();
	$o = '<ul style="margin-left:12px">';
	foreach($all_categories as $key => $cat) {
		if($cat->parent == "0") $o .= __show_category($cat, $selected_cats);
	}
	return $o . '</ul>';
}
function __show_category($cat_object, $selected_cats = null) {
	$checked = "";
	if(!is_null($selected_cats) && is_array($selected_cats)) {
		$checked = (in_array($cat_object->cat_ID, $selected_cats)) ? 'checked="checked"' : "";
	}
	$ou = '<li><label><input ' . $checked .' type="checkbox" name="cats[]" value="'. $cat_object->cat_ID .'" /> ' . $cat_object->cat_name . '</label>';
	$childs = get_categories('parent=' . $cat_object->cat_ID);
	foreach($childs as $key => $cat) {
		$ou .= '<ul style="margin-left:12px">' . __show_category($cat, $selected_cats) . '</ul>';
	}
	$ou .= '</li>';
	return $ou;
}

Initial URL
http://www.undolog.com

Initial Description

                                

Initial Title
Wordpress, show category with checkbox

Initial Tags
php, wordpress

Initial Language
PHP