21 * |
21 * |
22 * @since 2.8.0 |
22 * @since 2.8.0 |
23 */ |
23 */ |
24 public function __construct() { |
24 public function __construct() { |
25 $widget_ops = array( |
25 $widget_ops = array( |
26 'classname' => 'widget_recent_entries', |
26 'classname' => 'widget_recent_entries', |
27 'description' => __( 'Your site’s most recent Posts.' ), |
27 'description' => __( 'Your site’s most recent Posts.' ), |
28 'customize_selective_refresh' => true, |
28 'customize_selective_refresh' => true, |
29 ); |
29 ); |
30 parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops ); |
30 parent::__construct( 'recent-posts', __( 'Recent Posts' ), $widget_ops ); |
31 $this->alt_option_name = 'widget_recent_entries'; |
31 $this->alt_option_name = 'widget_recent_entries'; |
32 } |
32 } |
65 * @see WP_Query::get_posts() |
65 * @see WP_Query::get_posts() |
66 * |
66 * |
67 * @param array $args An array of arguments used to retrieve the recent posts. |
67 * @param array $args An array of arguments used to retrieve the recent posts. |
68 * @param array $instance Array of settings for the current widget. |
68 * @param array $instance Array of settings for the current widget. |
69 */ |
69 */ |
70 $r = new WP_Query( apply_filters( 'widget_posts_args', array( |
70 $r = new WP_Query( |
71 'posts_per_page' => $number, |
71 apply_filters( |
72 'no_found_rows' => true, |
72 'widget_posts_args', |
73 'post_status' => 'publish', |
73 array( |
74 'ignore_sticky_posts' => true, |
74 'posts_per_page' => $number, |
75 ), $instance ) ); |
75 'no_found_rows' => true, |
|
76 'post_status' => 'publish', |
|
77 'ignore_sticky_posts' => true, |
|
78 ), |
|
79 $instance |
|
80 ) |
|
81 ); |
76 |
82 |
77 if ( ! $r->have_posts() ) { |
83 if ( ! $r->have_posts() ) { |
78 return; |
84 return; |
79 } |
85 } |
80 ?> |
86 ?> |
89 <?php |
95 <?php |
90 $post_title = get_the_title( $recent_post->ID ); |
96 $post_title = get_the_title( $recent_post->ID ); |
91 $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' ); |
97 $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' ); |
92 ?> |
98 ?> |
93 <li> |
99 <li> |
94 <a href="<?php the_permalink( $recent_post->ID ); ?>"><?php echo $title ; ?></a> |
100 <a href="<?php the_permalink( $recent_post->ID ); ?>"><?php echo $title; ?></a> |
95 <?php if ( $show_date ) : ?> |
101 <?php if ( $show_date ) : ?> |
96 <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span> |
102 <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span> |
97 <?php endif; ?> |
103 <?php endif; ?> |
98 </li> |
104 </li> |
99 <?php endforeach; ?> |
105 <?php endforeach; ?> |
111 * WP_Widget::form(). |
117 * WP_Widget::form(). |
112 * @param array $old_instance Old settings for this instance. |
118 * @param array $old_instance Old settings for this instance. |
113 * @return array Updated settings to save. |
119 * @return array Updated settings to save. |
114 */ |
120 */ |
115 public function update( $new_instance, $old_instance ) { |
121 public function update( $new_instance, $old_instance ) { |
116 $instance = $old_instance; |
122 $instance = $old_instance; |
117 $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
123 $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
118 $instance['number'] = (int) $new_instance['number']; |
124 $instance['number'] = (int) $new_instance['number']; |
119 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; |
125 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false; |
120 return $instance; |
126 return $instance; |
121 } |
127 } |
122 |
128 |
123 /** |
129 /** |
129 */ |
135 */ |
130 public function form( $instance ) { |
136 public function form( $instance ) { |
131 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
137 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
132 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; |
138 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; |
133 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; |
139 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; |
134 ?> |
140 ?> |
135 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
141 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
136 <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> |
142 <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> |
137 |
143 |
138 <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> |
144 <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> |
139 <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> |
145 <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> |
140 |
146 |
141 <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' ); ?>" /> |
147 <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' ); ?>" /> |
142 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p> |
148 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p> |
143 <?php |
149 <?php |
144 } |
150 } |
145 } |
151 } |