0

The code:

$cats = get_terms(array(
            'taxonomy' => 'category',
            'hide_empty' => false,
    ));
for ($i=0; $i < count($cats); $i++) { 
            printf(
                    '<option value="%s" %s style="margin-bottom:3px;">%s</option>',
                    $cats[$i]->term_id,
                    in_array( $cats[$i]->term_id, $categ) ? 'selected="selected"' : '',
                    $cats[$i]->name
            );
        }   

var_dumping the $categ variable: array(2) { [0]=> string(1) "5" [1]=> string(1) "2" }

when i say var_dumping it means var_dumping everywhere, outside the for loop, inside, always array.
Can someone tell me how in hell PHP is giving me this warning??

EDIT

The whole form() method: and the var_dump is as you see, before the p tag with the fields and also after the foreach loop, both outputting an array

    if ( isset( $instance[ 'title' ] ) ) {
        $title = $instance[ 'title' ]; }
    else {
        $title = __( 'Latest Posts', 'di-news-blog' );
    }
    if ( isset( $instance ['numPost'] ) ) {
        $numPost = $instance[ 'numPost' ]; }
    else {
        $numPost = __( 3 , 'di-news-blog' );
    }
    if ( isset( $instance ['categ'] ) ) {
        $categ = $instance[ 'categ' ];
         }
    else {
        $categ = __( 'All categories' , 'di-news-blog' );
    }
    $cats = get_terms(array(
            'taxonomy' => 'category',
            'hide_empty' => false,
    ));
    // Widget admin form
     var_dump($categ); ?>
    <p>
    <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'di-news-blog' ); ?></label> 
    <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
    <label for="<?php echo $this->get_field_id( 'numPost' ); ?>"><?php _e( 'Number of posts:' , 'di-news-blog' ); ?></label> 
    <input style="margin-top: 15px" class="tiny-text" id="<?php echo $this->get_field_id( 'numPost' ); ?>" name="<?php echo $this->get_field_name( 'numPost' ); ?>" type="number" step="1" min="1" max="10" value="<?php echo esc_attr( $numPost ); ?>" size="3"><br><br>
    <label for="<?php echo $this->get_field_id( 'categ' ); ?>"><?php _e( 'Select categories:' , 'di-news-blog' ); ?></label> 
    <select  multiple style="height:200px" id="<?php echo $this->get_field_id( 'categ' ); ?>" name="<?php echo $this->get_field_name( 'categ' );?>[]">
        <option value="" style="margin-bottom:3px;"><?php _e('All categories', 'di-news-blog') ?></option>
    <?php 
        for ($i=0; $i < count($cats); $i++) { 
            printf(
                    '<option value="%s" %s style="margin-bottom:3px;">%s</option>',
                    $cats[$i]->term_id,
                    in_array( $cats[$i]->term_id, $categ) ? 'selected="selected"' : '',
                    $cats[$i]->name
            );
         }
         var_dump($categ);  ?>  

    </select>
    </p>
    <?php 
}
3
  • 1
    Post the code where $categ is initialised, and any/all interactions with it between then and the code above. Also, what's the var_dump($categ) output immediately above the printf statement? Commented Mar 21, 2018 at 21:37
  • Post edited with the whole form() method with two var_dumps, both outputting an array. Even if I choose the "All categories" option it's an array of one element. Commented Mar 22, 2018 at 2:09
  • You're setting $categ = __( 'All categories' , 'di-news-blog' );. This is a string, not an array. Commented Mar 22, 2018 at 2:15

1 Answer 1

1

Based on your updated post, when a category is not selected, you set $categ to a string value, and this is the cause of the warning.

$categ = __( 'All categories' , 'di-news-blog' );

Without knowing more about the purpose of the <select> field, it's difficult to advise further. But one option might be to set $categ to an empty array where not category is selected.

Sign up to request clarification or add additional context in comments.

2 Comments

But the problem occurred with categories selected and the variable being an array.... ¿? As I told you even without categories selected the variable is an array[0]=>''; Anyways I'll try that tomorrow and log back then. Thanks!!
You got it fubar!!. SOLVED. I really couldn't imagine an error happening because of a portion of code inside an if statement that is not being executed. But apparently that matters in the end.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.