0

I want a list like in the pictue; witch is here - Posts->New->Select Categories

WordPress Add New Post, Categories

At the moment i have this

echo '<select name="pn-cats[]" size="8" style="width: 100%" multiple>';
$args = array(
    'type'                     => 'post',
'child_of'                 => 0,
'parent'                   => '',
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => 0,
'hierarchical'             => 1,
'exclude'                  => '',
'include'                  => '',
'number'                   => '',
'taxonomy'                 => 'category',
'pad_counts'               => false 
); 
$categories = get_categories( $args ); 
foreach ($categories as $category) {
    $option = '<option value="'.$category->cat_name.'">';
    $option .= $category->cat_name;
    $option .= ' ('.$category->count.')';
    $option .= '</option>';
    echo $option;
}
echo '</select>';

But i dont like my solution because the categories are not sorted like parent->child in the picture. I need a multi-selectable list.

Do you know any solution? wp_category_checklist don´t work because i need this list in a public page and not in the admin area.

3
  • Have you tried using wp_dropdown_categories? codex.wordpress.org/Function_Reference/wp_dropdown_categories
    – shanebp
    Commented Feb 1, 2015 at 0:01
  • @shanebp: Why would he use wp_dropdown_categories when he wants a checklist? And for wassereimer: Who told you admin functions are not available in frontend? all you need to do is wrap it in a nice shortcode and snatch the css from the dashboard.
    – tao
    Commented Feb 1, 2015 at 2:02
  • @AndreiGheorghiu I read about this problem on a few sides.... Thats what a few people said.... How does this work? I´ve tried a few things but it won´t work..... Commented Feb 1, 2015 at 3:16

1 Answer 1

3

You can make wp_category_checklist() available in any of your templates using:

require_once( ABSPATH . '/wp-admin/includes/template.php' );
wp_category_checklist();

If you would like to have a shortcode for it, that parses the available arguments, here's an example:

    add_shortcode('frontend-category-checklist', 'frontend_category_checklist');
function frontend_category_checklist($atts) {

    // process passed arguments or assign WP defaults
    $atts = shortcode_atts( array(
        'post_id' => 0,
        'descendants_and_self' => 0,
        'selected_cats' => false,
        'popular_cats' => false,
        'checked_ontop' => true
    ), $atts, 'frontend-category-checklist'
    );

    // string to bool conversion, so the bool params work as expected
    $atts['selected_cats'] = to_bool( $atts['selected_cats'] );
    $atts['popular_cats'] = to_bool( $atts['popular_cats'] );
    $atts['checked_ontop'] = to_bool( $atts['checked_ontop'] );

    // load template.php from admin, where wp_category_checklist() is defined
    require_once( ABSPATH . '/wp-admin/includes/template.php' );

    // generate the checklist
    ob_start(); ?>
    <div class="categorydiv">
        <ul class="category-tabs">
            <div id="taxonomy-category" class="categorydiv">
                <div id="category-all" class="tabs-panel">
                    <ul id="categorychecklist" data-wp-lists="list:category" class="categorychecklist form-no-clear">
                        <?php wp_category_checklist(
                            $atts['post_id'],
                            $atts['descendants_and_self'],
                            $atts['selected_cats'],
                            $atts['popular_cats'],
                            null,
                            $atts['checked_ontop']
                        );  ?>
                    </ul>
                </div>
             </div>
        </ul>
    </div>

    <?php
    return ob_get_clean();
}
function to_bool($bool) {
    return ( is_bool($bool) ? $bool :
        ( is_numeric($bool) ? ((bool)intval($bool)) : $bool !== 'false' ) );
}

Now you can use [frontend-category-checklist] in any page or post. All arguments work, except for $walker.

Updated answer: I've made a fiddle with the bulk CSS that you need in order to make it look like the list in dashboard. The checkboxes don't work right in fiddle, but they work on a WordPress frontend (4.1). You are free to clean up the CSS of rules you don't actually need, I didn't have time for that.

NOTE that I have also updated the function in the answer to also generate some more html around the list. You should probably replace the old code with the new one.

7
  • Thank you for your solution! :) This works - but not in a custom page template. Must search why - but i will find it. Couldn´t i rum arguments directly through wp_category_checklist($args)? Commented Feb 1, 2015 at 16:33
  • Of course you could.
    – tao
    Commented Feb 1, 2015 at 16:36
  • I edited the _var2bool helper a few times and had 2 typos. Updated it. Should work now.
    – tao
    Commented Feb 1, 2015 at 16:39
  • This works fine, thank you again. But do you have an idea about the css? I´ve searched for the needed code in the tons of css files and code buti couldn´t find it -.- Commented Feb 2, 2015 at 16:06
  • I've made a fiddle with the bulk CSS. Perhaps you should rearrange it and clean up all rules that are not needed. Updated answer.
    – tao
    Commented Feb 2, 2015 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.