创建wordpress Widget多个实例

时间:2013-10-25 20:23:15

标签: php wordpress plugins widget wordpress-plugin

我创建了一个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");'));

我该怎么做?谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

  2. form()中添加标记以使输入具有“模板”,并由jQuery用于复制字段。

  3. update()中,迭代已发布的字段并相应保存。

  4. 资源:

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

相关问题