Reputation: 669
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
}
Upvotes: 0
Views: 126
Reputation: 17398
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.
Upvotes: 1