我试图创建一个小部件,在侧栏中显示帖子,并将帖子ID作为用户的输入。
我的代码的结果是,小部件只显示第一个输入的ID,而忽略其他ID。
示例:如果我在小部件中的输入是1236、1234、1235,小部件前端将只显示id为1236的帖子。
这是我的小部件的代码:
<?php
/*
Plugin Name: Post in Sidebar
Plugin URI:
Description:
Author:
Version: 1.0
Author URI:
*/
class postInSidebar extends WP_Widget {
public function __construct() { // Create the Widget
parent::__construct(
\'postInSidebar\', // ID
\'Post in Sidebar\', // Name
array( \'description\' => \'Post in Sidebar lets you choose some App Reviews and show them in the sidebar\' )
); // Description
}
public function widget( $args, $instance ) { // Frontend
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance[\'title\']) ? \' \' : apply_filters(\'widget_title\', $instance[\'title\']);
if (!empty($title))
echo $before_title . $title . $after_title;
// start postInSidebar frontend
$wp_query = new WP_Query( array (
\'post_type\' => \'post\',
\'post__in\' => array($instance[\'postsToShow\']),
\'orderby\' => \'post__in\'
));
?>
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?> - <?php the_ID(); ?>
</a>
</h2>
</div>
<?php endwhile; else: ?>
<p>No posts yet, go to appearance -> widgets and choose which posts to show here.</p>
<?php endif; ?>
<?php wp_reset_postdata();
// end postInSidebar frontend
echo $after_widget;
}
public function update( $new_instance, $old_instance ) { // Update
$instance = $old_instance;
$new_instance = wp_parse_args((array) $new_instance, array(
\'title\' => \'\',
\'postsToShow\' => \'\'
));
$instance[\'title\'] = strip_tags($new_instance[\'title\']);
$instance[\'postsToShow\'] = strip_tags($new_instance[\'postsToShow\']);
return $instance;
}
public function form( $instance ) { // Backend
$instance = wp_parse_args( (array) $instance, array(
\'title\' => \'\',
\'postsToShow\' => \'\'
));
$title = esc_attr($instance[\'title\']);
$postsToShow = esc_attr($instance[\'postsToShow\']);
?>
<!-- title -->
<p>
<label for="<?php echo $this->get_field_id(\'title\'); ?>">
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 esc_attr($title); ?>" />
</label>
</p>
<!-- Posts To Show -->
<p>
<label for="<?php echo $this->get_field_id(\'postsToShow\'); ?>">
Posts To Show:
<input class="widefat" id="<?php echo $this->get_field_id(\'postsToShow\'); ?>" name="<?php echo $this->get_field_name(\'postsToShow\'); ?>" type="text" value="<?php echo esc_attr($postsToShow); ?>" />
</label>
</p>
<?php }
}
add_action( \'widgets_init\', function(){
register_widget( \'postInSidebar\' );
});
最合适的回答,由SO网友:birgire 整理而成
尝试替换
\'post__in\' => array($instance[\'postsToShow\']),
与
\'post__in\' => explode(",", $instance[\'postsToShow\']),
为
post__in
使用数组。
您当前的阵列如下所示
array("123,456,789")
但应该是这样的
array("123", "456", "789")