Advertisement
deliciousthemes

Recent Posts Widget

Mar 31st, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. <?php
  2. /******************************************
  3. /* Recent Posts Widget
  4. ******************************************/
  5. class dt_recent_posts extends WP_Widget {
  6.                            
  7.     /** constructor */
  8.     function dt_recent_posts() {
  9.         parent::WP_Widget(false, $name = 'Line - Recent Posts');
  10.     }
  11.  
  12.     /** @see WP_Widget::widget */
  13.     function widget($args, $instance) {    
  14.         extract( $args );
  15.         $title = apply_filters('widget_title', $instance['title']);
  16.         $number = apply_filters('widget_title', $instance['number']);
  17.             echo $before_widget;
  18.                 if ( $title )
  19.                     echo $before_title . $title . $after_title;
  20.                         $recPosts = new WP_Query();
  21.                         $recPosts->query('showposts='.$number.'');
  22.                             while ($recPosts->have_posts()) : $recPosts->the_post(); ?>                            
  23.                                 <div class="sidebar-post">
  24.                                     <a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ){?><?php the_post_thumbnail('sidebar-thumb');}else{?><img src="<?php echo get_template_directory_uri(); ?>/images/article-replacer.jpg" alt="article" /></a><?php } ?><h5><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h5><span><?php the_time(get_option('date_format')); ?> / <?php comments_popup_link(__('No Comments', 'line'), __('1 Comment', 'line'), __('% Comments', 'line')); ?></span>                   
  25.                                 </div><!--end sidebar-post-->
  26.                                
  27.                             <?php endwhile;
  28.                         wp_reset_query();
  29.                     echo $after_widget;
  30.     }
  31.  
  32.     /** @see WP_Widget::update */
  33.     function update($new_instance, $old_instance) {            
  34.     $instance = $old_instance;
  35.     $instance['title'] = strip_tags($new_instance['title']);
  36.     $instance['number'] = strip_tags($new_instance['number']);
  37.         return $instance;
  38.     }
  39.  
  40.     /** @see WP_Widget::form */
  41.     function form($instance) {
  42.         $instance = wp_parse_args( (array) $instance, array( 'title' => 'Recent Posts', 'number'=> 2));        
  43.         $title = esc_attr($instance['title']);
  44.         $number = esc_attr($instance['number']);
  45.         ?>
  46.          <p>
  47.           <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title: ', 'line'); ?></label>
  48.           <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; ?>" />
  49.         </p>
  50.         <p>
  51.           <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of Posts to Show:', 'line'); ?></label>
  52.           <input class="widefat" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
  53.         </p>
  54.         <?php
  55.     }
  56.  
  57. } // class dt_recent_posts
  58. // register Recent Posts widget
  59. add_action('widgets_init', create_function('', 'return register_widget("dt_recent_posts");')); 
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement