3 * Widget API: WP_Widget_Recent_Posts class
11 * Core class used to implement a Recent Posts widget.
17 class WP_Widget_Recent_Posts extends WP_Widget {
20 * Sets up a new Recent Posts widget instance.
25 public function __construct() {
26 $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "Your site’s most recent Posts.") );
27 parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
28 $this->alt_option_name = 'widget_recent_entries';
32 * Outputs the content for the current Recent Posts widget instance.
37 * @param array $args Display arguments including 'before_title', 'after_title',
38 * 'before_widget', and 'after_widget'.
39 * @param array $instance Settings for the current Recent Posts widget instance.
41 public function widget( $args, $instance ) {
42 if ( ! isset( $args['widget_id'] ) ) {
43 $args['widget_id'] = $this->id;
46 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
48 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
49 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
51 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
54 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
57 * Filter the arguments for the Recent Posts widget.
61 * @see WP_Query::get_posts()
63 * @param array $args An array of arguments used to retrieve the recent posts.
65 $r = new WP_Query( apply_filters( 'widget_posts_args', array(
66 'posts_per_page' => $number,
67 'no_found_rows' => true,
68 'post_status' => 'publish',
69 'ignore_sticky_posts' => true
72 if ($r->have_posts()) :
74 <?php echo $args['before_widget']; ?>
76 echo $args['before_title'] . $title . $args['after_title'];
79 <?php while ( $r->have_posts() ) : $r->the_post(); ?>
81 <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
82 <?php if ( $show_date ) : ?>
83 <span class="post-date"><?php echo get_the_date(); ?></span>
88 <?php echo $args['after_widget']; ?>
90 // Reset the global $the_post as this query will have stomped on it
97 * Handles updating the settings for the current Recent Posts widget instance.
102 * @param array $new_instance New settings for this instance as input by the user via
104 * @param array $old_instance Old settings for this instance.
105 * @return array Updated settings to save.
107 public function update( $new_instance, $old_instance ) {
108 $instance = $old_instance;
109 $instance['title'] = sanitize_text_field( $new_instance['title'] );
110 $instance['number'] = (int) $new_instance['number'];
111 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
116 * Outputs the settings form for the Recent Posts widget.
121 * @param array $instance Current settings.
123 public function form( $instance ) {
124 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
125 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
126 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
128 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
129 <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>
131 <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
132 <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>
134 <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' ); ?>" />
135 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>