我开发了一个侧边栏小部件,显示所选类别的最新帖子,现在我想通过此小部件显示来自多个类别的最新帖子 .
所以,我希望有类别的复选框(在管理面板中的窗口小部件窗体内),以便能够选择多个类别并显示所有这些选定类别的最新帖子 .
谢谢你的帮助 .
这是我的小部件代码:
<?php
add_action('widgets_init', 'home_featured_in_init');
function home_featured_in_init()
{
register_widget('home_featured_in');
}
class home_featured_in extends WP_Widget{
function home_featured_in()
{
$widget_ops = array('classname' => 'home_featured_in', 'description' => 'Home featured in');
$control_ops = array('id_base' => 'home_featured_in_widget');
$this->WP_Widget('home_featured_in_widget', 'Gunner: home_featured_in ', $widget_ops, $control_ops);
}
// content of this function will be showd in the page:
function widget($args, $instance)
{
extract($args);
$title = $instance['title'];
$post_type = 'all';
$categories = $instance['categories'];
$posts = $instance['posts'];
echo $before_widget;
?>
<?php
$features = new WP_Query(array(
'showposts' => $posts,
'post_type' => $post_type_array,
'cat' => $categories,
));
?>
<section class="featured-in">
<div class="container">
<div class="row">
<h5 class="text-center"><?php echo $title; ?></h5>
</div>
<div class="row">
<?php if ($features->have_posts()): while ($features->have_posts()):
$features->the_post(); ?>
<div class="features col-lg-4 col-md-4 col-sm-12 col-xs-12">
<a href="<?php echo get_the_content(); ?>" target="_blank">
<img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" class="center-block img-responsive">
</a>
</div>
<?php endwhile; endif; ?>
</div>
</div><!-- .container -->
</section> <!-- .featured-in -->
<?php
echo $after_widget;
}
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = $new_instance['title'];
$instance['post_type'] = 'all';
$instance['categories'] = $new_instance['categories'];
$instance['posts'] = $new_instance['posts'];
return $instance;
}
function form($instance)
{
$defaults = array(
'title' => 'Capabilities posts', 'post_type' => 'all', 'categories' => 'all', 'posts' => 3,
);
$instance = wp_parse_args((array) $instance, $defaults); ?>
<h3>Column One</h3>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
<input class="widefat" style="width: 216px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('categories'); ?>">Filter by Category:</label>
<select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" class="widefat categories" style="width:100%;">
<option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>>
all categories
</option>
<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
<?php foreach($categories as $category) { ?>
<option value='<?php echo $category->term_id; ?>' <?php if ($category->term_id == $instance['categories']) echo 'selected="selected"'; ?>>
<?php echo $category->cat_name; ?>
</option>
<?php } ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('posts'); ?>">Number of posts:</label>
<input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('posts'); ?>" name="<?php echo $this->get_field_name('posts'); ?>" value="<?php echo $instance['posts']; ?>" />
</p>
<?php }
}
?>