首页 wordpress插件

今天给大家分享一个小工具,可以在后台调用指定分类的新文章。不用插件,直接粘贴在function.php中即可。

?class RandomPostWidget extends WP_Widget 
{ 
 function RandomPostWidget() 
 { 
 parent::WP_Widget('bd_random_post_widget', '调用指定ID的新文章', array('description' => '调用指定ID的新文章') ); 
 } 
 
 function widget($args, $instance)
 
 {
 $args = array(
 'before_widget' => '<aside class="widget widget_recent_entries">',
 'after_widget' => '</aside>',
 'before_title' => '<h5 class="widget_title">',
 'after_title' => '</h5>'
); 
 extract( $args );
 
 
 $title = apply_filters('widget_title',empty($instance['title']) ? 'Last News' : $instance['title'], $instance, $this->id_base);
 $catid = apply_filters('widget_title',empty($instance['catid']) ? 'Last News' : $instance['catid'], $instance, $this->id_base);
 if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
 { 
 $number = 10;
 } 
 
 $r = new WP_Query(array('cat'=>$catid,'posts_per_page' => $number, 'no_found_rows' => true,
'post_status' => 'publish', 'ignore_sticky_posts' => true, 'orderby' =>'date')); 
 if ($r->have_posts())
 { 
 echo "\n"; 
 echo $before_widget;
 if ( $title ) echo $before_title . $title . $after_title;
 ?> 
<ul> 
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a><span class="post-date"><?php the_time("m d , y"); ?></span></li> 
<?php endwhile; ?> 
</ul><?php 
 echo $after_widget;
 wp_reset_postdata(); 
 } 
 } 
 
 function update($new_instance, $old_instance)
 { 
 $instance = $old_instance;
 $instance['title'] = strip_tags($new_instance['title']);
 $instance['catid'] = strip_tags($new_instance['catid']);
 $instance['number'] = (int) $new_instance['number'];
 return $instance;
 } 
 
 function form($instance)
 { 
 $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
 $catid = isset($instance['catid']) ? esc_attr($instance['catid']) : '';
 $number = isset($instance['number']) ? absint($instance['number']) : 10;?>
 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('标题:'); ?></label>
 <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
 <p><label for="<?php echo $this->get_field_id('catid'); ?>"><?php _e('CatID:'); ?></label>
 <input id="<?php echo $this->get_field_id('catid'); ?>" name="<?php echo $this->get_field_name('catid'); ?>" type="text" value="<?php echo $catid; ?>" /></p>
 <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('数量:'); ?></label> 
 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
<?php 
 } 
 
} 
add_action('widgets_init', create_function('', 'return register_widget("RandomPostWidget");'));

游客回答:(0)

声明:本站所有主题/文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除,谢谢!
©www.sbmzenith.com 2013-2024 All Rights Reserved.
豫ICP备15009393号