I have a widget that I need to add a category select list to. The end user should be able to select a category and I need to save the category ID with the widget.
I've hit a stumbling block in that I cannot get my exclude array to work. The excluded cats still show up in the dropdown. What am I doing wrong?
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'hide_title' => '', 'category_id' => '' ) );
$title = format_to_edit($instance['title']);
$text = format_to_edit($instance['text']);
$hide_title = $instance['hide_title'] ? ' checked="checked"' : '';
$category_id = $instance['category_id'] ? ' selected="selected"' : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" class="widefat" />
</p>
<select id="<?php echo $this->get_field_id( 'category_id' ); ?>" name="<?php echo $this->get_field_name( 'category_id' ); ?>">
<option value=""><?php echo esc_attr(__('Select a Category')); ?></option>
<?php
$args = array('exclude' => array(get_cats()),'hide_empty' => 0 );
$categories=get_categories($args);
foreach ($categories as $category) {
$option = '<option value="'.$category->cat_ID.'">';
$option .= $category->cat_name;
$option .= '</option>';
echo $option;
}
?>
</select>
function get_cats(){
$exclude_cats = array(
get_cat_ID('test1'),
get_cat_ID('test2'),
get_cat_ID('test3'),
);
return $exclude_cats;
}