Wordpress, show category with checkbox


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Restituisce una lista (UL/LI) delle categorie
  3.  * @params $selected_cats Array delle categorie selezionate
  4.  *
  5.  * @return HTML
  6.  */
  7. function get_categories_checkboxes( $selected_cats = null ) {
  8. $all_categories = get_categories();
  9. $o = '<ul style="margin-left:12px">';
  10. foreach($all_categories as $key => $cat) {
  11. if($cat->parent == "0") $o .= __show_category($cat, $selected_cats);
  12. }
  13. return $o . '</ul>';
  14. }
  15. function __show_category($cat_object, $selected_cats = null) {
  16. $checked = "";
  17. if(!is_null($selected_cats) && is_array($selected_cats)) {
  18. $checked = (in_array($cat_object->cat_ID, $selected_cats)) ? 'checked="checked"' : "";
  19. }
  20. $ou = '<li><label><input ' . $checked .' type="checkbox" name="cats[]" value="'. $cat_object->cat_ID .'" /> ' . $cat_object->cat_name . '</label>';
  21. $childs = get_categories('parent=' . $cat_object->cat_ID);
  22. foreach($childs as $key => $cat) {
  23. $ou .= '<ul style="margin-left:12px">' . __show_category($cat, $selected_cats) . '</ul>';
  24. }
  25. $ou .= '</li>';
  26. return $ou;
  27. }

URL: http://www.undolog.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.