为了在WP 4.0中制作自己的小部件,我一直在关注大量教程和codex页面。我似乎离得很近;我可以从小部件中输出一些内容,但不能输出任何字段。
在横幅中。小部件。php我有以下内容:
class bannerWidget extends WP_Widget {
public $id_base = \'banner_widget\';
public $name = \'Banner Widget\';
public $widget_options = array(
\'classname\' => \'RandomPostWidget\',
\'description\' => \'Displays a random post with thumbnail\'
);
public $control_options = array();
function bannerWidget() {
parent::WP_Widget(false, $name = __(\'Banner Widget\', \'banner_widget\') );
}
function form($instance) {
// Check values
if( $instance) {
$title = esc_attr($instance[\'title\']);
$text = esc_attr($instance[\'text\']);
$textarea = esc_textarea($instance[\'textarea\']);
} else {
$title = \'\';
$text = \'\';
$textarea = \'\';
}
?>
<p>
<label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Widget Title\', \'wp_widget_plugin\'); ?></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(\'text\'); ?>"><?php _e(\'Text:\', \'wp_widget_plugin\'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id(\'text\'); ?>" name="<?php echo $this->get_field_name(\'text\'); ?>" type="text" value="<?php echo $text; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'textarea\'); ?>"><?php _e(\'Textarea:\', \'wp_widget_plugin\'); ?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id(\'textarea\'); ?>" name="<?php echo $this->get_field_name(\'textarea\'); ?>"><?php echo $textarea; ?></textarea>
</p>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance[\'title\'] = strip_tags($new_instance[\'title\']);
$instance[\'text\'] = strip_tags($new_instance[\'text\']);
$instance[\'textarea\'] = strip_tags($new_instance[\'textarea\']);
return $instance;
}
public function widget($args, $instance){
extract( $args );
// these are the widget options
$title = apply_filters(\'widget_title\', $instance[\'title\']);
$text = $instance[\'text\'];
$textarea = $instance[\'textarea\'];
echo $before_widget;
// Display the widget
echo \'<div class="widget-text wp_widget_plugin_box">\';
// Check if title is set
if ( $title ) {
echo $before_title . $title . $after_title;
}
// Check if text is set
if( $text ) {
echo \'<p class="wp_widget_plugin_text">\'.$text.\'</p>\';
}
// Check if textarea is set
if( $textarea ) {
echo \'<p class="wp_widget_plugin_textarea">\'.$textarea.\'</p>\';
}
echo \'</div>\';
echo $after_widget;
}
}
add_action(\'widgets_init\', create_function(\'\', \'return register_widget("bannerWidget");\')
在函数中。php:
function make_sidebar_area() {
register_sidebar( array(
\'name\' => \'Test Sidebar Area\',
\'id\' => \'test_sidebar_1\',
\'description\' => \'Test sidebar description here\',
\'before_widget\' => \'<div>\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2 class="rounded">\',
\'after_title\' => \'</h2>\',
)
);
}
add_action( \'widgets_init\', \'make_sidebar_area\' );
最后是索引。php我有:
the_widget(\'bannerWidget\');
这将输出以下内容,但未显示任何填充的小部件字段:
<div class="widget widget_bannerwidget">
<div class="widget-text wp_widget_plugin_box">
</div>
</div>
谁能告诉我哪里出了问题?
最合适的回答,由SO网友:Eric Holmes 整理而成
使用the_widget()
否认这些$instance
变量被设置,当您将其粘贴到后端的侧栏中时,通常是这样。
尝试以下操作:
the_widget(\'bannerWidget\', array(
\'title\' => \'WordPress rocks!\',
\'text\' => \'Hello World.\',
\'textarea\' => \'Some content for the textarea\'
) );
the_widget
用于硬编码小部件。要使用放置在自定义侧栏中的,请替换
the_widget()
使用此选项:
echo \'<ul class="sidebar">\';
dynamic_sidebar( \'test_sidebar_1\' );
echo \'</ul>\';
dynamic_sidebar
用于显示指定侧栏内的所有小部件(传递侧栏ID),其中
the_widget
仅用于呈现特定的小部件,通常仅用于没有需要编辑的选项的对象。