简体   繁体   中英

Wordpress Category Sidebar

In the word press admin panel there is a category selection 'helper panel' which I am trying to recreate. However I can't find the code for it, could someone point me in the right direction please?

例

The actual Category Box that is created by Wordpress is not typically used by Plugins that utilize a Custom UI. However, you can mimic its behavior, and you were most certainly on the right track with get_categories(). If you want to grab ALL categories, not just the ones with a post count, you need to call it like so:

<?php
$args = array(
'type'                     => 'post',
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => 0, //<--IMPORTANT!!
'hierarchical'             => 1,
'taxonomy'                 => 'category',
'pad_counts'               => false );
$categories = get_categories($args);
?>

'hide_empty' is what you were missing. Once you want to create your checkboxes, you would do something like this:

<form action="action.php" method="POST">
<?php
foreach($categories as $cat)
{
    echo '<input type="checkbox" name="categories[]" value="'.$cat->cat_ID.'" />';
    echo '<label>'$cat->name.'</label><br/>';
}
?>
<input type="text" name="user_input" value="" />
</form>

You can style the checkboxes however you like using a custom stylesheet , or you can apply the same tags and classes that the standard one uses, which will ensure that the existing Wordpress Admin Stylesheet styles everything accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM