我制作了一个带有输入字段的小部件。我想制作一个添加更多按钮来动态添加输入字段第一个输入字段是默认值。我怎样才能做到这一点?
以下是我的小部件代码:
<?php
class ExampleClasss extends WP_Widget {
function __construct() {
// Widget settings
$widget_ops = array( \'classname\' => \'exampleclass\', \'description\' => esc_html__( \'This is Just a practice widget\', \'ex\' ) );
// Create the widget
parent::__construct( \'exampleclass\', __(\'Example widget\', \'ex\'), $widget_ops );
}
function widget( $args, $instance ) {
extract( $args, EXTR_SKIP );
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$link1 = empty( $instance[\'link1\'] ) ? \'\' : $instance[\'link1\'];
echo $before_widget;
if ($title)
echo $before_title . $title . $after_title; ?>
<div class=""><?php echo wp_kses_post( $link1 ); ?></div>
<?php
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'title\'] = sanitize_text_field( $new_instance[\'title\'] );
$instance[\'link1\'] = sanitize_text_field( $new_instance[\'link1\'] );
return $instance;
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( \'Title\' => esc_html__( \'About Me\', \'ex\' ) ) );
$title = ( !empty($instance[\'title\']) ? esc_attr( $instance[\'title\'] ) : \'Title\' );
$link1 = ( !empty($instance[\'link1\']) ? esc_textarea( $instance[\'link1\'] ) : \'Link 1\' );
# T I T L E
$output = "<p>";
$output .= "<label for=\'" . esc_attr( $this->get_field_id(\'title\') ) . "\'>".esc_html__( \'Title\', \'ex\')."</label>";
$output .= "<input type=\\"text\\" class=\\"widefat\\" id=\'". esc_attr( $this->get_field_id( \'title\' ) ) ."\' name=\'". esc_attr( $this->get_field_name(\'title\') ) ."\' value=\'".$title."\'></p>";
# A B O U T M E T E X T
$output .= "<p>";
$output .= "<label for=\'" . esc_attr( $this->get_field_id(\'link1\') ) . "\'>".esc_html__( \'Link 1 \' , \'ex\' )."</label>×";
$output .= "<input type=\\"text\\" class=\\"widefat\\" id=\'". esc_attr( $this->get_field_id( \'link1\' ) ) ."\' name=\'". esc_attr( $this->get_field_name(\'link1\') ) ."\' value=\'".$link1."\'></p>";
echo $output;
}
}
function Example() {
register_widget( \'ExampleClasss\' );
}
add_action( \'widgets_init\', \'Example\' );
?>
这是我的截图。希望这能澄清我的问题!