【问题标题】:Create wordpress Widget multiple instances创建 wordpress Widget 多个实例
【发布时间】:2013-10-25 20:23:14
【问题描述】:

我创建了一个 wordpress 小部件(一个有 2 个字段的表单)并将其显示在后台..

一切正常,我可以用它只创建一条记录并将其显示在前面,但我真正想做的是能够使用它从后台创建许多记录(如帖子)并将它们展示给网站的前台。

前台发布记录:(我想要的)

Record1               |     Record2               |     Record3
-------------------------------------------------------------------------------
Title:    Title1      |     Title:    Title2      |     Title:    Title3
Price:    Price1      |     Price:    Price2      |     Price:    Price3

前台发布记录:(我实际拥有的)

Record               
----------------------
Title:    Title      
Price:    Price      

这是我的代码:

class wp_ikbal extends WP_Widget {

    // constructor
    function wp_ikbal() {
        parent::WP_Widget(false, $name = __('Ikbal Widget', 'wp_ikbal') );
    }

    // widget form creation
    function form($instance) {  
        // Check values
        if( $instance) {
            $Title = esc_attr($instance['text']);
            $Price = esc_textarea($instance['textarea']);
        } else {
            $Title = '';
            $Price = '';
        }
        ?>

        <p>
            <label for="<?php echo $this->get_field_id('Title'); ?>"><?php _e('Nom Produit:', 'wp_ikbal'); ?></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; ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('Price'); ?>"><?php _e('Price:', 'wp_ikbal'); ?></label>
            <textarea class="widefat" id="<?php echo $this->get_field_id('Price'); ?>" name="<?php echo $this->get_field_name('Price'); ?>"><?php echo $Price; ?></textarea>
        </p>
        <?php
    }

    // widget update
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        // Fields
        $instance['Title'] = strip_tags($new_instance['Title']);
        $instance['Price'] = strip_tags($new_instance['Price']);
        return $instance;
    }

    // widget display
    function widget($args, $instance) {
        extract( $args );
        // these are the widget options
        $text = $instance['Title'];
        $textarea = $instance['Price'];
        echo $before_widget;
        // Display the widget
        echo '<div class="widget-text wp_widget_plugin_box">';

        // Check if text is set
        if( $Title ) {
            echo '<p class="wp_widget_plugin_text">'.$Title.'</p>';
        }

        // Check if textarea is set
        if( $Price ) {
            echo '<p class="wp_widget_plugin_textarea">'.$Price.'</p>';
        }

        echo '</div>';
        echo $after_widget;
    }
}

// register widget
add_action('widgets_init', create_function('', 'return register_widget("wp_ikbal");'));

我该怎么做?谢谢。

【问题讨论】:

    标签: php plugins widget wordpress


    【解决方案1】:

    您需要可重复字段,这里的答案将是 this one in WPSE 的逐字记录。基本上:

    1. 在 Widget 构造函数中添加 admin_enqueue_scripts 以加载所需的 jQuery(或者您可以将其内嵌在 form() 中)。

    2. form() 中添加标记,以便为输入提供一个“模板”,并且 jQuery 使用该模板来复制字段。

    3. update() 中,遍历发布的字段并相应地保存。

    资源:

    PS:你可能会发现当 Widget 第一次被放到侧边栏时 jQuery 不起作用。解决方案是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 2018-07-11
      • 2017-11-15
      • 2020-07-08
      相关资源
      最近更新 更多