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 custom Walker for a Advanced Search, that outputs the categories for multiple search by categories in checkbox. The Problem here is, that i use the get_category_children for the children Categories, but its old, and the new code is get_term_children, but when i replace it with it, then comes a error message, like

Warning: substr() expects parameter 1 to be string, object given in /..../search-fields.php on line 269

But the Search Functionality works.

The Code for the Walker Class is:

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";
}
}

How can i replace the old with the new term_children. Thanks everyone for the help.

    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);
    }
share|improve this question

1 Answer 1

get_term_children returns an array or an object (WP_Error) and you are treating it like a string when you try to feed it to substr. I am not sure what you are trying to do with that line of code but in general you need to check that you are dealing with an array and not an error and process accordingly.

$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

share|improve this answer
    
Thank you, i have changed the Code [code] $cat_children = get_term_children( $category->term_id, 'category' ); if (is_wp_error($cat_children)) { echo 'Error'; } elseif (is_array($cat_children)) {.....}[/code] And now its work, and no error comes more. With the code i want output the categorys in Checkbox, so that one can choose in which category/or categorys will search: In Category A & B & C/ or only A. The rest of the code have i now posted in my question :) And sry for my bad english. –  jamal Jan 15 '13 at 15:46
    
What i will is, that the ID of the Children Category is display here onchange='toggle( {$category->term_id}, Array( $cat_children, 999 ) )' So that its child category checked when the parent is checked (java), but with get_term_children is give only a array back, only the own id is shown, not that of his child category. –  jamal Jan 15 '13 at 16:19

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.