遵循教程,效果很好。我只是不明白update方法是如何从Select标记在数据库中保存$instance数组的?您可以在管理区域中选择默认或完全、平滑保存,并将其保存在db中。
public function widget( $args, $instance ) {
echo $args[\'before_widget\'];
echo \'<div class="g-ytsubscribe"
data-layout="\'.$instance[\'data_layout\'].\'"
data-count="default">
</div>\';
echo $args[\'after_widget\'];
}
public function form( $instance ) {
$data_layout = ! empty( $instance[\'data_layout\'] ) ? $instance[\'data_layout\'] : esc_html__( \'default\', \'yts_domain\' );
?>
<select class="widefat"
<option value="default" <?php echo ($data_layout == \'default\' ? \'selected\' : \'\'); ?>>Default</option>
<option value="full" <?php echo ($data_layout == \'full\' ? \'selected\' : \'\'); ?>>Full</option>
</select>
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'data_layout\'] = ( ! empty( $new_instance[\'data_layout\'] ) ) ? sanitize_text_field( $new_instance[\'data_layout\'] ) : \'\';
return $instance;
}