我有一个我买的 Wordpress 主题,它附带了许多不同的小部件。我有一个带有以下代码的代码,它的作用是显示最近的帖子。我需要对其进行更改,实际上是两个,而不是向我显示最近的 POST,我需要它来向我显示最近的投资组合项目,并且只显示某个类别。
<?php
// =============================== My Recent Posts (News widget) ======================================
class MY_PostWidget extends WP_Widget {
/** constructor */
function MY_PostWidget() {
parent::WP_Widget(false, $name = 'My - Recent Posts');
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$category = apply_filters('widget_category', $instance['category']);
$linktext = apply_filters('widget_linktext', $instance['linktext']);
$linkurl = apply_filters('widget_linkurl', $instance['linkurl']);
$count = apply_filters('widget_count', $instance['count']);
?>
<?php echo $before_widget; ?>
<?php if ( $title )
echo $before_title . $title . $after_title; ?>
<?php $temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(); ?>
<ul class="latestpost">
<?php $querycat = $category; ?>
<?php $wp_query->query("showposts=". $count ."&category_name=". $querycat); ?>
<?php if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();?>
<li>
<?php if(has_post_thumbnail()) {
echo '<figure class="featured-thumbnail"><a href="'; the_permalink(); echo '">';
echo the_post_thumbnail('small-post-thumbnail');
echo '</a></figure>';
}
?>
<time datetime="<?php the_time('Y-m-d\TH:i'); ?>"><a href="<?php the_permalink(); ?>"><?php the_time('[m-d-Y]'); ?></a></time>
<?php $excerpt = get_the_excerpt(); echo my_string_limit_words($excerpt,13);?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php $wp_query = null; $wp_query = $temp;?>
<!-- Link under post cycle -->
<?php if($linkurl !=""){?>
<a href="<?php echo $linkurl; ?>" class="button"><?php echo $linktext; ?></a>
<?php } ?>
<?php echo $after_widget; ?>
<?php
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
return $new_instance;
}
/** @see WP_Widget::form */
function form($instance) {
$title = esc_attr($instance['title']);
$category = esc_attr($instance['category']);
$linktext = esc_attr($instance['linktext']);
$linkurl = esc_attr($instance['linkurl']);
$count = esc_attr($instance['count']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('Category Slug:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo $category; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Posts per page:'); ?><input class="widefat" style="width:30px; display:block; text-align:center" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="text" value="<?php echo $count; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('linktext'); ?>"><?php _e('Link Text:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linktext'); ?>" name="<?php echo $this->get_field_name('linktext'); ?>" type="text" value="<?php echo $linktext; ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('Link Url:', 'theme1472'); ?> <input class="widefat" id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" type="text" value="<?php echo $linkurl; ?>" /></label></p>
<?php
}
} // class Widget
?>
这是它现在在小部件窗格中的样子: