简体   繁体   中英

How to correctly modify this Wordpress Function for widgets?

I understand the below function for Wordpress widgets:

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>',
    ));

as before_title and after_title are my H2 titles of the widget, and the before_widget and after_widget encases the widget itself.

But what if I need to put a class inside the widget, instead of outside? I want all content inside the widget to have a certain class. What would the correct syntax be for this ( inside_widget )?

May be you need this This sample code creates a Widget named FooWidget that has a settings form to change the display title.

/**
 * 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");'));

May be you need this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM