Very short snippet: Wordpress, list categories with checkboxes

A few days ago I released a new major release (the 2.3.0) of the WordPress plugin WP Bannerize , for last year. In addition to having introduced the use of widgets (which I hope to write something in the near future) or "finally" allowed to filter out the banners for class.
Widgets in the section I found the need to view the categories of the blog so that they can be selected via a control checkbox! In addition, I had to show "indented" in respect of the parent / child hierarchy. Having had a look (I admit fast) to the functions of WordPress, it seemed that there was anything already available and documented!

Note: actually, when we insert a new post in the backend Wordress, the page offers a list of their categories indented with a lot of checkboxes! , solo che queste sono funzioni interne, quindi meglio evitare di usarle), ho optato per scriverne una mia personale However, not wanting to perform any reverse engineering to recover the function (I admit ... I did and I also found, see wp_category_checklist() in your wp-admin , only that these are internal functions, so avoid using them) I decided to write my own personal

So I wrote a personal function that displays the categories of a blog in an indented list adding a checkbox control. This function can be inserted into functions.php . The function, in fact, has two functions: main and service used to "iterate" recursively.
In the version I present myself (WordPress uses in these cases his object - or rather its class - Walker ) I have not used the nested function , as it gave some problems with the structure of campatibilità WordPress widgets, but you can re-scale them to any other purposes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/ **
* Returns a list (UL / LI) of the categories
* @ Params array $ selected_cats selected categories
*
* @ Return HTML
* /
$selected_cats = null ) { get_categories_checkboxes function ($ selected_cats = null) {
get_categories ( ) ; $ All_categories get_categories = ();
'<ul style="margin-left:12px">' ; $ O = '<ul style="margin-left:12px">';
$all_categories as $key => $cat ) { foreach ($ all_categories as $ key => $ cat) {
$cat -> parent == "0" ) $o .= __show_category ( $cat , $selected_cats ) ; if ($ cat -> parent == "0") or $ __show_category .= ($ cat, $ selected_cats);
}
. '</ul>' ; return $ o. '</ ul>';
}

$cat_object , $selected_cats = null ) { __show_category function (cat_object $, $ selected_cats = null) {
"" ; $ Checked = "";
! is_null ( $selected_cats ) && is_array ( $selected_cats ) ) { if ( is_null ($ selected_cats) & & is_array ($ selected_cats)) {
( in_array ( $cat_object -> cat_ID , $selected_cats ) ) ? 'checked="checked"' : "" ; $ Checked = ( in_array ($ cat_object -> cat_id, $ selected_cats))? 'checked = "checked"': "";
}
'<li><label><input ' . $checked . ' $ U = '<li> <label> <input'. $ Checked. ' $cat_object -> cat_ID . '" /> ' . $cat_object -> cat_name . '</label>' ; type = "checkbox" name = "cats []" value = "'. cat_object $ -> cat_id.'" /> '. cat_object $ -> cat_name.' </ label> ';

get_categories ( 'parent=' . $cat_object -> cat_ID ) ; $ Childs = get_categories ('parent ='. Cat_object $ -> cat_id);
$childs as $key => $cat ) { foreach ($ childs as $ key => $ cat) {
'<ul style="margin-left:12px">' . __show_category ( $cat , $selected_cats ) . '</ul>' ; $ Ou .= '<ul style="margin-left:12px">'. __show_category ($ Cat, $ selected_cats). '</ Ul>';
}
'</li>' ; $ Ou .= '</ li>';
; return $ u;
}

A comment: "Very short snippet: Wordpress, list categories with checkboxes"

  1. December 5, 2009 The best of the week # 41 | BigThink :

    [...] Very short snippet: Wordpress, list categories with checkboxes How to display the list of categories, accompanied by a checkbox. [...]

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA