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
}
$categis initialised, and any/all interactions with it between then and the code above. Also, what's thevar_dump($categ)output immediately above theprintfstatement?$categ = __( 'All categories' , 'di-news-blog' );. This is astring, not anarray.