Php 如何在wordpress中制作多选小部件?
我的Wordpress插件有问题。我试图制作一个按钮小部件,它在后端有多个选项。我的问题是正在保存标题,但没有保存选择标记中的选项。 下面是一段代码:Php 如何在wordpress中制作多选小部件?,php,wordpress,Php,Wordpress,我的Wordpress插件有问题。我试图制作一个按钮小部件,它在后端有多个选项。我的问题是正在保存标题,但没有保存选择标记中的选项。 下面是一段代码: <code>add_action( 'widgets_init', function(){ register_widget( 'Button_Widget' ); }); class Button_Widget extends WP_Widget { /** * Register widget with WordP
<code>add_action( 'widgets_init', function(){
register_widget( 'Button_Widget' );
});
class Button_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'Button_Widget', // Base ID
__('♦ Button', 'text_domain'), // Name
array('description' => __( 'Insert a button', 'text_domain' ),) // Args
);
}
/**
* Front-End Widget
*/
public function widget( $args, $instance ) {
//coming soon
}
/**
* Back-end widget form.
*/
public function form( $instance ) {
?>
}您需要向select元素添加一个id,然后在小部件内实现更新功能:
function update($new, $old){
$instance = $old;
$instance["select_id_1"] = $new["select_id_1"];
return $instance;
}
我找到了解决办法 以下是WordPress后端的代码:
<p>
<label for="<?php echo $this->get_field_id('text'); ?>">Color:
<select class='widefat' id="<?php echo $this->get_field_id('color'); ?>"
name="<?php echo $this->get_field_name('color'); ?>" type="text">
<option value='Green'<?php echo ($color=='Green')?'selected':''; ?>>
Green
</option>
<option value='Red'<?php echo ($color=='Red')?'selected':''; ?>>
Red
</option>
<option value='Blue'<?php echo ($color=='Blue')?'selected':''; ?>>
Blue
</option>
</select>
</label>
</p>
为了确保一切正常,您还可以输出以下值:
if (!empty($animation))
echo $animation;
if (!empty($color))
echo $color;
我在类Button_Widget{}中添加了te update函数,将WP_Widget{}扩展到此位置,并通过两种方式向select元素添加了一个id。有一次我这样加上:。。。另一种方式是这样的:淡入淡出,但没有一种方式有助于保存选项。
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['animation'] = $new_instance['animation'];
$instance['color'] = $new_instance['color'];
return $instance;
}
if (!empty($animation))
echo $animation;
if (!empty($color))
echo $color;