GET_CATEGORY_CHILDS/新的GET_TERM_CHILDS不起作用

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

我有一个用于高级搜索的自定义助行器,它通过复选框中的类别输出多个搜索的类别。这里的问题是,我将get\\u category\\u children用于children类别,但它是旧的,而新代码是get\\u term\\u children,但当我将其替换为它时,会出现一条错误消息,如

Warning: substr() expects parameter 1 to be string, object given in /..../search-fields.php on line 269
但搜索功能是有效的。

Walker类的代码为:

class Multiple_Walker_Category extends Walker 
{

var $tree_type = \'category\';
var $db_fields = array (\'parent\' => \'parent\', \'id\' => \'term_id\');

function start_lvl(&$output, $depth, $args) {

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

}

function end_lvl(&$output, $depth, $args) {
$indent = str_repeat("\\t", $depth);
$output .= "$indent</ul>\\n";

}

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

        $cat_name = apply_filters( \'list_cats\', $cat_name, $category );
        $cat_children = substr(**get_category_children**( $category->term_id, \'\', \', \'), 0, -2 );

        $output .= "<li><input type=\'checkbox\' onchange=\'toggle( {$category->term_id}, Array( $cat_children, 999 ) )\' class=\'cb-element\'  checked=\'checked\' name=\'cat_{$category->term_id}\' id=\'cat_{$category->term_id}\' />&nbsp;";

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

}

function end_el(&$output, $category, $depth, $args) {
$output .= "</li>\\n";
}
}
我怎样才能用新的term\\u children替换旧的。谢谢大家的帮助。

    function adv_form_cats($args = \'\')
    {
        $defaults = array(
            \'show_option_all\' => \'\',
            \'orderby\' => \'name\',
            \'order\' => \'ASC\', 
                        \'show_last_update\' => 0,
            \'style\' => \'list\', 
                        \'show_count\' => 0,
            \'hide_empty\' => 1, 
                        \'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\' id=\'checkAll\' checked=\'checked\' name=\'checkall\'  />&nbsp;";

        $output .= "<label for=\'checkall\'><span></span>".__(\'Check 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 .= walk_category_new_tree($categories, $depth, $r);
        }

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

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




    return $output;
    }


function walk_category_new_tree()
    {
        $walker = new Multiple_Walker_Category;
        $args = func_get_args();
        return call_user_func_array(array(&$walker, \'walk\'), $args);
    }

1 个回复
SO网友:s_ha_dum

get_term_children 返回一个数组或对象(WP\\u Error),当您尝试将其馈送到substr. 我不确定您想用这行代码做什么,但通常需要检查您处理的是数组,而不是错误,并相应地进行处理。

$cat_children = get_category_children( $category->term_id, \'\', \', \');
if (is_wp_error($cat_children)) {
  // do stuff
} elseif (is_array($cat_children)) {
  // do other stuff
  // I don\'t really know what you want to do here. 
  // Your code would, if I am reading right, chop two characters off of a string
  // which strikes me as a odd thing to do given the data you dealing with
}

http://php.net/manual/en/function.substr.php

http://codex.wordpress.org/Function_Reference/is_wp_error

http://php.net/manual/en/function.is-array.php

结束

相关推荐

在小部件选项中使用wp_Dropdown_Categories

我有一个产品搜索小部件,当您选择某些产品时,会显示一个隐藏字段。我想在我的小部件选项中设置一个自定义类别,然后用于显示隐藏字段。当我将父category\\u id手动添加到代码中时,前端可以100%工作。现在我只需要在后端设置category选项。这是我目前拥有的,但它不起作用,因为它没有存储所做的选择。我省略了搜索表单,因为它没有使用任何小部件选项。class Equipment_Search extends WP_Widget { function Equipment_Se