我正在构建一个简单的小部件,它允许用户从一系列帖子中选择一篇帖子
出于某种原因,当我选择一个选项时,我相信它实际上已被选中,但该选项不会保留在“选择”字段中。
我想我的错误在下面几行,但我没有设法纠正它。也许调用\\u ID不是这样做的方式…:
<option value="<? echo the_ID(); ?>" <?php selected(the_ID(), $instance[\'post_to_display\']); ?>><?php the_title(); ?></option>
这是我的代码:
class Simple_Widget extends WP_Widget{
function Simple_Widget() {
$widget_ops = array(
\'classname\' => \'w_reports\',
\'description\' => __(\'Use this widget to add a post to the sidebar\', \'theme\'));
$this->WP_Widget(\'w_reports\', __(\'Simple_Widget\', \'roots\'), $widget_ops);
}
function widget($args, $instance) {
extract( $args );
$post_to_display = $instance[\'post_to_display\'];
}
function update($new_instance, $old_instance) {
$instance[\'post_to_display\'] = $new_instance[\'post_to_display\'];
return $instance;
}
function form($instance) {
?>
<p>
<label for="<?php echo $this->get_field_id(\'post_to_display\'); ?>">Choose a post to display:</label>
<select id="<?php echo $this->get_field_id(\'post_to_display\'); ?>" name="<?php echo $this->get_field_name(\'post_to_display\'); ?>" class="widefat">
<?php // My query
$the_query = new WP_Query( \'post_type=my-post\' );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<option value="<? echo the_ID(); ?>" <?php selected(the_ID(), $instance[\'post_to_display\']); ?>><?php the_title(); ?></option>
<?php endwhile;
wp_reset_postdata();
?>
</select>
</p>
// Just a test
<?php echo $instance[\'post_to_display\']; ?>
我做错了什么?提前感谢您的帮助!