我理解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_title
和after_title
是我的H2
小部件标题,before_widget
和after_widget
包含小部件本身。
但是如果我需要在窗口小部件中放置一个类而不是外部呢?我希望窗口小部件中的所有内容都有一个特定的类。这个(inside_widget
)的正确语法是什么?
答案 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");'));
可能你需要这个