我坚持使用我的代码。保存所选页面或将所选选项设置为selected=selected
时遇到一些问题。在我看来,这段代码应该可行,但它没有,它不会更新选项。
有人有想法或解决方案吗?
感谢。 :)
<?php
add_action( 'widgets_init', create_function( '', 'register_widget( "ok_bubble" );' ) );
function ok_enqueue_bubble_widget_scripts() {
if ( is_active_widget( false, false, 'ok_bubble', true ) ) {
//wp_enqueue_script('validate');
}
}
add_action('wp_enqueue_scripts', 'ok_enqueue_bubble_widget_scripts');
/**
* Adds Foo_Widget widget.
*/
class ok_bubble extends WP_Widget {
/**
* Register widget with WordPress.
*/
public function __construct() {
$widget_ops = array('classname' => 'widget_bubble', 'description' => __('Write your information in a bubble.'));
parent::__construct('ok_bubble', __('Bubble'), $widget_ops);
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
extract($args);
$current_page = $this->_get_current_page($instance);
$link = get_post($current_page);
echo $before_widget;
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<div class="bubblewidget" data-rev="<?php $link->post_title; ?>"></div>
<?php
echo $after_widget;
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['page'] = stripslashes($new_instance['page']);
return $instance;
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( ) );
$current_page = $this->_get_current_page($instance);
?>
<p><label for="<?php echo $this->get_field_id('page'); ?>"><?php _e('Leistung verlinken:') ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('page'); ?>" name="<?php echo $this->get_field_name('page'); ?>">
<option value=""><?php echo esc_attr( __( 'Seite auswählen' ) ); ?></option>
<?php
$postlist = get_posts( 'posts_per_page=-1&post_type=skills' );
$posts = array();
foreach ( $postlist as $post ) :
$option = '<option value="' . get_page_link( $post->ID ) . '"' . selected($page, $current_page) . '>';
$option .= $post->post_title;
$option .= '</option>';
echo $option;
endforeach; ?>
</select></p>
<?php
}
function _get_current_page($instance) {
return $instance['page'];
}
} // class Foo_Widget
?>
答案 0 :(得分:1)
只需更改两行:
添加$page
变量,然后用它更新值。因此,您的foreach
帖子列表应为:
<?php
$postlist = get_posts( 'posts_per_page=-1&post_type=skills' );
$posts = array();
foreach ( $postlist as $post ) :
$page = get_page_link( $post->ID );
$option = '<option value="' . $page . '"' . selected($page, $current_page, false) . '>';
$option .= $post->post_title;
$option .= '</option>';
echo $option;
endforeach;
?>