【问题标题】:Create a wordpress widget with multiple textarea创建具有多个文本区域的 wordpress 小部件
【发布时间】:2012-10-17 19:38:29
【问题描述】:
class ad_widget extends WP_Widget {
function __construct() {
    $widget_ops = array( 'classname' => 'ad-widget-container', 'description' => __( ' Ad WIDGET' ) );
    parent::__construct( 'ad-widget', __( 'Ad widget', 'test' ), $widget_ops );
}
function widget( $args, $instance ) {
    extract( $args, EXTR_SKIP );
    $title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', $instance['title'] );
    $text = empty( $instance['text'] ) ? '' : apply_filters( 'widget_text', $instance['text'] );
    echo $args['before_widget'];
    if ( $title )
        echo $args['before_title'] . $title . $args['after_title'];

    echo '<div class="cutom-widget">';
       echo $text;
    echo '</div>';
    echo $args['after_widget'];
}
function update( $new_instance, $old_instance ) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['text'] =  $new_instance['text'];
    return $instance;
}    
function form( $instance ) {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    $title = esc_attr( $instance['title'] ); 
    $text = esc_textarea($instance['text']); ?>
    <p>
       <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'test' ); ?></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 $title; ?>" />
        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
    *<a>Add more text area</a>
    <a>delete text area </a>*
    </p><?php
}

} 这是一个带有文本框和文本区域的简单小部件。当我单击“添加更多文本区域”时,它会添加一个具有不同 ID 的文本区域并保存不同的值。

【问题讨论】:

    标签: php javascript jquery wordpress


    【解决方案1】:

    我猜你必须使用 JS 来做到这一点。

    1. 在最后一个文本区域放置或附加按钮
    2. 点击按钮后会添加另一个带有适当标记的文本区域
    3. 获取该值并附加到小部件表单上

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 2014-04-01
      • 2023-03-17
      相关资源
      最近更新 更多