选中类别Parent,其ID来自Childs-Get_Term_Child

时间:2013-01-17 作者:jamal

我有一个层次分类列表,如:

A (Parent Category)

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

B (Parent Category)

 - E (Child Category)
 - F (Child Category)
如果父类别有子类别,那么父类别也有子类别的ID,如

<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 ) )">
因此,当检查父类别时,也会检查子类别。

Wordpress的旧函数get\\u children\\u categories按预期工作,java通过识别id来检查孩子。

但我的问题是,函数get\\u children\\u categories被取消了,而新函数get\\u term\\u children不起作用。

旧代码看起来是这样的:

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 ) )\'

.................
但当我更改代码时,如:

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

"<li><input type=\'checkbox\' onchange=\'toggle( {$category->term_id}, Array( $children...
它只给出0分。打印时,\\r会输出具有子类别id的数组,如:

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

 $children = print_r( $children );
输出为:

 Array ( [0] => 3 [1] => 4 ) Array ( ) Array ( ) Array ( [0] => 6 ) Array ( ) 
有人知道我如何发布它吗?我已经做了几个小时了,没有主动提出来。或者,也可以有另一种解决方案来检查子类别。谢谢你的帮助。。。

1 个回复
SO网友:jamal

Sry,以下是我的答案:

以下是在复选框中列出类别的第一个输出:

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);
    }
然后下载这个http://code.google.com/p/checkboxtree/

然后通过div初始化此插件,在其中创建输出in复选框,如:

$(\'#adv_search_form .multicheck-cats\').tree({ components: [\'checkbox\', \'collapse\'], onCheck: { node: \'expand\', ancestors: \'checkIfFull\', descendants: \'check\' }, onUncheck: { node: \'collapse\', ancestors: \'uncheck\' } });
以下是现场预览:

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

结束

相关推荐

在editags.php中使用多个id过滤分类术语

我想使用编辑标签。具有多个术语id的php搜索函数。例如,如果我在搜索框中键入以下内容:#1245&;6832,它应该在结果表(WP\\U terms\\U List\\U table)中显示这两个术语。我尝试使用pre\\u get\\u posts操作访问正在运行的搜索查询,但这只是显示一个“空”查询:add_action(\'pre_get_posts\', \'filter_terms_by_ids\' ); function filter_terms_by_ids( $w