如何正确修改小部件的Wordpress功能?

时间:2009-11-05 19:15:39

标签: php wordpress

我理解Wordpress小部件的以下功能:

register_sidebar(array(
        'name' => 'sidebar',
        'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widgetTop"></div>',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="accordion_toggle">',
        'after_title' => '</h2>',
    ));

before_titleafter_title是我的H2小部件标题,before_widgetafter_widget包含小部件本身。

但是如果我需要在窗口小部件中放置一个类而不是外部呢?我希望窗口小部件中的所有内容都有一个特定的类。这个(inside_widget)的正确语法是什么?

1 个答案:

答案 0 :(得分:0)

可能你需要这个 此示例代码创建一个名为FooWidget的Widget,其中包含一个用于更改显示标题的设置表单。

/**
 * FooWidget Class
 */
class FooWidget extends WP_Widget {
    /** constructor */
    function FooWidget() {
        parent::WP_Widget(false, $name = 'FooWidget');  
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {     
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>
                  Hello, World!
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {             
        return $new_instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {              
        $title = esc_attr($instance['title']);
        ?>
            <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <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; ?>" /></label></p>
        <?php 
    }

} // class FooWidget
// register FooWidget widget
add_action('widgets_init', create_function('', 'return register_widget("FooWidget");'));

可能你需要这个