WP_Widget_Recent_Posts

Core class used to implement a Recent Posts widget.

Defined (1)

The class is defined in the following location(s).

/wp-includes/widgets/class-wp-widget-recent-posts.php  
  1. class WP_Widget_Recent_Posts extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up a new Recent Posts widget instance. 
  5. * @since 2.8.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $widget_ops = array( 
  10. 'classname' => 'widget_recent_entries',  
  11. 'description' => __( 'Your site’s most recent Posts.' ),  
  12. 'customize_selective_refresh' => true,  
  13. ); 
  14. parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops ); 
  15. $this->alt_option_name = 'widget_recent_entries'; 
  16.  
  17. /** 
  18. * Outputs the content for the current Recent Posts widget instance. 
  19. * @since 2.8.0 
  20. * @access public 
  21. * @param array $args Display arguments including 'before_title', 'after_title',  
  22. * 'before_widget', and 'after_widget'. 
  23. * @param array $instance Settings for the current Recent Posts widget instance. 
  24. */ 
  25. public function widget( $args, $instance ) { 
  26. if ( ! isset( $args['widget_id'] ) ) { 
  27. $args['widget_id'] = $this->id; 
  28.  
  29. $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' ); 
  30.  
  31. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  32. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 
  33.  
  34. $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; 
  35. if ( ! $number ) 
  36. $number = 5; 
  37. $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; 
  38.  
  39. /** 
  40. * Filters the arguments for the Recent Posts widget. 
  41. * @since 3.4.0 
  42. * @see WP_Query::get_posts() 
  43. * @param array $args An array of arguments used to retrieve the recent posts. 
  44. */ 
  45. $r = new WP_Query( apply_filters( 'widget_posts_args', array( 
  46. 'posts_per_page' => $number,  
  47. 'no_found_rows' => true,  
  48. 'post_status' => 'publish',  
  49. 'ignore_sticky_posts' => true 
  50. ) ) ); 
  51.  
  52. if ($r->have_posts()) : 
  53. ?> 
  54. <?php echo $args['before_widget']; ?> 
  55. <?php if ( $title ) { 
  56. echo $args['before_title'] . $title . $args['after_title']; 
  57. } ?> 
  58. <ul> 
  59. <?php while ( $r->have_posts() ) : $r->the_post(); ?> 
  60. <li> 
  61. <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a> 
  62. <?php if ( $show_date ) : ?> 
  63. <span class="post-date"><?php echo get_the_date(); ?></span> 
  64. <?php endif; ?> 
  65. </li> 
  66. <?php endwhile; ?> 
  67. </ul> 
  68. <?php echo $args['after_widget']; ?> 
  69. <?php 
  70. // Reset the global $the_post as this query will have stomped on it 
  71. wp_reset_postdata(); 
  72.  
  73. endif; 
  74.  
  75. /** 
  76. * Handles updating the settings for the current Recent Posts widget instance. 
  77. * @since 2.8.0 
  78. * @access public 
  79. * @param array $new_instance New settings for this instance as input by the user via 
  80. * WP_Widget::form(). 
  81. * @param array $old_instance Old settings for this instance. 
  82. * @return array Updated settings to save. 
  83. */ 
  84. public function update( $new_instance, $old_instance ) { 
  85. $instance = $old_instance; 
  86. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  87. $instance['number'] = (int) $new_instance['number']; 
  88. $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; 
  89. return $instance; 
  90.  
  91. /** 
  92. * Outputs the settings form for the Recent Posts widget. 
  93. * @since 2.8.0 
  94. * @access public 
  95. * @param array $instance Current settings. 
  96. */ 
  97. public function form( $instance ) { 
  98. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 
  99. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; 
  100. $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; 
  101. ?> 
  102. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
  103. <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; ?>" /></p> 
  104.  
  105. <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> 
  106. <input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /></p> 
  107.  
  108. <p><input class="checkbox" type="checkbox"<?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" /> 
  109. <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p> 
  110. <?php