Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a hierarchical category list, like:

A (Parent Category)

 - B (Child Category)
 - C (Child Category)
 - D (Child Category)

B (Parent Category)

 - E (Child Category)
 - F (Child Category)

And i will, that if the Parent Category has Children Categorys, that then the Parent Category have the ID'S also of the Child Categorys, like

<input id="parent_1(ID)" class="checkbox" type="checkbox" name="parent_1(ID)" checked="checked" onchange="toggle( 1 (ID of the Parent Category), Array( 4, 3 (Both ID's of the Child Categorys), 999 ) )">

So that when Parent Category is check, the child Categorys also checked.

With the old function of Wordpress get_children_categories worked as expected, and java makes the rest with check the childrens by recognizing the id.

But my Problem is, that the function get_children_categories is depraceted and with the new function get_term_children its not work.

The old code looked so:

class Walker_Category extends Walker 
{
    var $tree_type = 'category';
    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');

    function start_lvl($output, $depth, $args) {
        if ( 'list' != $args['style'] )
            return $output;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent<ul class='children'>\n";
        return $output;
    }
....................

function start_el($output, $category, $depth, $args)
    {
        extract($args);
        $cat_name = esc_attr($category->name);

        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $children = substr( get_category_children( $category->term_id, '', ', '), 0, -2 );
li><input type='checkbox' onchange='toggle( {$category->term_id}, Array( $children, 999 ) )'

.................

But when i changed the code like:

$children = get_term_children( (int) $category->term_id, $category);

"<li><input type='checkbox' onchange='toggle( {$category->term_id}, Array( $children...

It gives only a 0 out. When i print_r than outputs arrays with the id's of the children categories, like:

 $children = get_term_children( (int) $category->term_id, 'category');

 $children = print_r( $children );

And the output is:

 Array ( [0] => 3 [1] => 4 ) Array ( ) Array ( ) Array ( [0] => 6 ) Array ( ) 

Does anyone have any idea how I can release it? I'm already a few hours on it and come not forward with it. Or there may be another solution whereby the sub-categories are also checked. Thanks for the help ...

share|improve this question

1 Answer 1

Sry, here is my answer:

Here is first the output for listing the category in checkbox:

Walker for the Category Output

class Adv_Multicheck_Walker_Category extends Walker {
    var $tree_type = 'category';
    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');

    function start_lvl($output, $depth, $args) {
        if ( 'list' != $args['style'] )
            return $output;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent<ul class='children'>\n";
        return $output;
    }

    function end_lvl($output, $depth, $args) {
        if ( 'list' != $args['style'] )
            return $output;

        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";

        return $output;
    }

    function start_el($output, $category, $depth, $args) {
        extract($args);
        $cat_name = esc_attr($category->name);
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $checked = '';
        if ( empty($_GET['s']) || $_GET["adv_cat_{$category->term_id}"] == 'on' )
                    $checked = 'checked="checked"';

        $output .= "<li><input type='checkbox' class='cb-element'  checked='checked' name='adv_cat_{$category->term_id}' id='adv_cat_{$category->term_id}' />&nbsp;";

        $output .= "<label for='adv_cat_{$category->term_id}'><span></span>$category->name</label>";

        if ( isset($show_count) && $show_count )
            $output .= ' (' . intval($category->count) . ')';

        if ( isset($show_date) && $show_date )
            $output  .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
    }

    function end_el($output, $page, $depth, $args) {
        if ( 'list' != $args['style'] )
            return $output;

        $output .= "</li>\n";
        return $output;
    }
}

function adv_cats($args = '') {
        $defaults = array(
            'show_option_all' => '?', 
            'orderby' => 'name',
            'order' => 'ASC', 
            'show_last_update' => 0,
            'style' => 'list', 
            'show_count' => 1,
            'hide_empty' => 0, 
            'use_desc_for_title' => 1,
            'child_of' => 0, 
            'feed' => '',
            'feed_image' => '', 
            'exclude' => '',
            'hierarchical' => true, 
            'title_li' => '',
            'echo' => 0
        );

        $r = wp_parse_args( $args, $defaults );

        if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
            $r['pad_counts'] = true;
        }

        if ( isset( $r['show_date'] ) ) {
            $r['include_last_update_time'] = $r['show_date'];
        }

        extract( $r );

        $categories = get_categories($r);

        $output = '';
        if ( $title_li && 'list' == $style )
                $output = '<li class="categories">' . $r['title_li'] . '<ul>';

        if ( empty($categories) ) {
            if ( 'list' == $style )
                $output .= '<li>' . __("No categories", "WPL") . '</li>';
            else
                $output .= __("No categories", "WPL");
        } else {
            global $wp_query;

                if( !empty($show_option_all) )
                if ('list' == $style )

        $output .= "<li><input type='checkbox' class='checkall'  checked='checked' name='checkall' id='checkall' />&nbsp;";

        $output .= "<label for='checkall'><span></span>".__('Check/Uncheck All', 'WPL').'</label>';

            if ( is_category() )
                $r['current_category'] = $wp_query->get_queried_object_id();

            if ( $hierarchical )
                $depth = 0;  // Walk the full depth.
            else
                $depth = -1; // Flat.

            $output .= $this->walk_category_tree($categories, $depth, $r);
        }

        if ( $title_li && 'list' == $style )
            $output .= '</ul></li>';

        $output = apply_filters('wp_list_categories', $output);

    return '<ul>' . $output . '</ul>';
    }

    function walk_category_tree() {
        $walker = new Adv_Multicheck_Walker_Category;
        $args = func_get_args();
        return call_user_func_array(array(&$walker, 'walk'), $args);
    }

Then download this http://code.google.com/p/checkboxtree/

Then init this plugin by the div where one create the output in checkbox, like:

$('#adv_search_form .multicheck-cats').tree({ components: ['checkbox', 'collapse'], onCheck: { node: 'expand', ancestors: 'checkIfFull', descendants: 'check' }, onUncheck: { node: 'collapse', ancestors: 'uncheck' } });

And here is a live Preview:

http://pro.at-tanzil.de/erweiterte-suche-2/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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