Advertisement
Guest User

Recent posts with image

a guest
Aug 13th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. class SQueen_Recent_Posts2_widget extends WP_Widget {
  2.  
  3. /**
  4. * Widget setup
  5. */
  6. function __construct() {
  7.  
  8. $widget_ops = array(
  9. 'description' => __( 'Recent posts with thumbnails widget.', 'kleo_framework' )
  10. );
  11. parent::__construct( 'kleo_recent_posts2', __('[Kleo] Recent posts2','kleo_framework'), $widget_ops );
  12. }
  13.  
  14. /**
  15. * Display widget
  16. */
  17. function widget( $args, $instance ) {
  18. extract( $args, EXTR_SKIP );
  19.  
  20. $title = apply_filters( 'widget_title', $instance['title'] );
  21. $limit = $instance['limit'];
  22. $length = (int)( $instance['length'] );
  23. $thumb = $instance['thumb'];
  24. $cat = $instance['cat'];
  25. $post_type = $instance['post_type'];
  26.  
  27. echo $before_widget;
  28.  
  29. if ( ! empty( $title ) )
  30. echo $before_title . $title . $after_title;
  31.  
  32. global $post;
  33.  
  34. if ( false === ( $kleo_recent_posts = get_transient( 'kleo_recent_posts2_' . $widget_id ) ) ) {
  35.  
  36. $args = array(
  37. 'numberposts' => $limit,
  38. 'cat' => $cat,
  39. 'post_type' => $post_type
  40. );
  41.  
  42. $kleo_recent_posts = get_posts( $args );
  43.  
  44. set_transient( 'kleo_recent_posts2_' . $widget_id, $kleo_recent_posts, 60*60*12 );
  45.  
  46. } ?>
  47.  
  48. <div>
  49.  
  50. <ul class='latest-blog'>
  51.  
  52. <?php foreach( $kleo_recent_posts as $post ) : setup_postdata( $post ); ?>
  53.  
  54. <li>
  55. <?php if( $thumb == true ) : ?>
  56. <span class='avatar'><a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail(); ?></a></span>
  57. <?php endif; ?>
  58. <p><?php the_title(); ?> <br/><?php echo word_trim(get_the_excerpt(), $length, '...' ); ?> <a href="<?php the_permalink(); ?>"><?php _e("read more", 'kleo_framework'); ?></a></p>
  59. </li>
  60. <?php endforeach; wp_reset_postdata(); ?>
  61.  
  62. </ul>
  63.  
  64. </div>
  65.  
  66. <?php
  67.  
  68. echo $after_widget;
  69.  
  70. }
  71.  
  72. /**
  73. * Update widget
  74. */
  75. function update( $new_instance, $old_instance ) {
  76.  
  77. $instance = $old_instance;
  78. $instance['title'] = esc_attr( $new_instance['title'] );
  79. $instance['limit'] = $new_instance['limit'];
  80. $instance['length'] = (int)( $new_instance['length'] );
  81. $instance['thumb'] = $new_instance['thumb'];
  82. $instance['cat'] = $new_instance['cat'];
  83. $instance['post_type'] = $new_instance['post_type'];
  84.  
  85. delete_transient( 'kleo_recent_posts_' . $this->id );
  86.  
  87. return $instance;
  88.  
  89. }
  90.  
  91. /**
  92. * Widget setting
  93. */
  94. function form( $instance ) {
  95.  
  96. /* Set up some default widget settings. */
  97. $defaults = array(
  98. 'title' => '',
  99. 'limit' => 5,
  100. 'length' => 10,
  101. 'thumb' => true,
  102. 'cat' => '',
  103. 'post_type' => '',
  104. 'date' => true,
  105. );
  106.  
  107. $instance = wp_parse_args( (array) $instance, $defaults );
  108. $title = esc_attr( $instance['title'] );
  109. $limit = $instance['limit'];
  110. $length = (int)($instance['length']);
  111. $thumb = $instance['thumb'];
  112. $cat = $instance['cat'];
  113. $post_type = $instance['post_type'];
  114.  
  115. ?>
  116.  
  117. <p>
  118. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'kleo_framework' ); ?></label>
  119. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo $title; ?>" />
  120. </p>
  121. <p>
  122. <label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php _e( 'Limit:', 'kleo_framework' ); ?></label>
  123. <select class="widefat" name="<?php echo $this->get_field_name( 'limit' ); ?>" id="<?php echo $this->get_field_id( 'limit' ); ?>">
  124. <?php for ( $i=1; $i<=20; $i++ ) { ?>
  125. <option <?php selected( $limit, $i ) ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
  126. <?php } ?>
  127. </select>
  128. </p>
  129. <p>
  130. <label for="<?php echo esc_attr( $this->get_field_id( 'length' ) ); ?>"><?php _e( 'Excerpt length:', 'kleo_framework' ); ?></label>
  131. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'length' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'length' ) ); ?>" type="text" value="<?php echo $length; ?>" />
  132. </p>
  133.  
  134. <?php if( current_theme_supports( 'post-thumbnails' ) ) { ?>
  135.  
  136. <p>
  137. <label for="<?php echo esc_attr( $this->get_field_id( 'thumb' ) ); ?>"><?php _e( 'Display Post Image?', 'kleo_framework' ); ?></label>
  138. <input id="<?php echo $this->get_field_id( 'thumb' ); ?>" name="<?php echo $this->get_field_name( 'thumb' ); ?>" type="checkbox" value="1" <?php checked( '1', $thumb ); ?> />&nbsp;
  139. </p>
  140.  
  141. <?php } ?>
  142.  
  143. <p>
  144. <label for="<?php echo esc_attr( $this->get_field_id( 'cat' ) ); ?>"><?php _e( 'Show from category: ' , 'kleo_framework' ); ?></label>
  145. <?php wp_dropdown_categories( array( 'name' => $this->get_field_name( 'cat' ), 'show_option_all' => __( 'All categories' , 'kleo_framework' ), 'hide_empty' => 1, 'hierarchical' => 1, 'selected' => $cat ) ); ?>
  146. </p>
  147. <p>
  148. <label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>"><?php _e( 'Choose the Post Type: ' , 'kleo_framework' ); ?></label>
  149. <select class="widefat" id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>">
  150. <?php foreach ( get_post_types( '', 'objects' ) as $post_type ) { ?>
  151. <option value="<?php echo esc_attr( $post_type->name ); ?>" <?php selected( $instance['post_type'], $post_type->name ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option>
  152. <?php } ?>
  153. </select>
  154. </p>
  155.  
  156. <?php
  157. }
  158.  
  159. }
  160.  
  161. /**
  162. * Register widget.
  163. *
  164. * @since 1.0
  165. */
  166. add_action( 'widgets_init', create_function( '', 'register_widget( "SQueen_Recent_Posts2_widget" );' ) );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement