我创建了一个自定义小部件,它应该显示一个包含博客所有类别的选择菜单。我使用get\\u categories来编译列表。这很好,所有类别都显示在下拉菜单中。每次我保存并刷新小部件页面时,自定义小部件就不再存在了。我检查过了function update
那里一切都很好。所以我想这一定是我创建表单的方式。有什么想法吗?提前谢谢。
我不想转储所有代码,所以我只粘贴了创建表单的函数。如果你需要更多,请发表评论
function form( $instance ) {
/* Default Widget Settings */
$defaults = array(
\'title\' => \'Highlight Category\',
\'select\'=> \'Option 1\'
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<!-- Widget Title -->
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Title:\', \'lang\') ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" />
</p>
<!-- Widget Article Count -->
<p>
<label for="<?php echo $this->get_field_id(\'select\'); ?>"><?php _e(\'This is a select menu\', \'lang\'); ?></label>
<select name="<?php echo $this->get_field_name(\'select\'); ?>" id="<?php echo $this->get_field_id(\'select\'); ?>" class="widefat">
<option value="<?php echo $this->get_field_name(\'select\'); ?>"><?php echo $instance[\'select\']; ?></option>
<?php
$categories= get_categories(\'child_of=0\');
foreach ($categories as $category) {
$option = \'<option value="\' . $category->cat_name . \'" id="\' . $this->get_field_id( \'select\' ) . \'">\';
$option .= $instance[\'select\'];
$option .= \' (\'. $this->get_field_id( \'select\' ) .\')\';
$option .= \'</option>\';
echo $option;
}
?>
</select>
</p>
<?php
}