43 public function widget( $args, $instance ) { |
43 public function widget( $args, $instance ) { |
44 if ( ! isset( $args['widget_id'] ) ) { |
44 if ( ! isset( $args['widget_id'] ) ) { |
45 $args['widget_id'] = $this->id; |
45 $args['widget_id'] = $this->id; |
46 } |
46 } |
47 |
47 |
48 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' ); |
48 $default_title = __( 'Recent Posts' ); |
|
49 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title; |
49 |
50 |
50 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
51 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
51 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
52 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
52 |
53 |
53 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; |
54 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; |
54 if ( ! $number ) { |
55 if ( ! $number ) { |
55 $number = 5; |
56 $number = 5; |
56 } |
57 } |
57 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; |
58 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false; |
58 |
59 |
59 /** |
|
60 * Filters the arguments for the Recent Posts widget. |
|
61 * |
|
62 * @since 3.4.0 |
|
63 * @since 4.9.0 Added the `$instance` parameter. |
|
64 * |
|
65 * @see WP_Query::get_posts() |
|
66 * |
|
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. |
|
69 */ |
|
70 $r = new WP_Query( |
60 $r = new WP_Query( |
|
61 /** |
|
62 * Filters the arguments for the Recent Posts widget. |
|
63 * |
|
64 * @since 3.4.0 |
|
65 * @since 4.9.0 Added the `$instance` parameter. |
|
66 * |
|
67 * @see WP_Query::get_posts() |
|
68 * |
|
69 * @param array $args An array of arguments used to retrieve the recent posts. |
|
70 * @param array $instance Array of settings for the current widget. |
|
71 */ |
71 apply_filters( |
72 apply_filters( |
72 'widget_posts_args', |
73 'widget_posts_args', |
73 array( |
74 array( |
74 'posts_per_page' => $number, |
75 'posts_per_page' => $number, |
75 'no_found_rows' => true, |
76 'no_found_rows' => true, |
82 |
83 |
83 if ( ! $r->have_posts() ) { |
84 if ( ! $r->have_posts() ) { |
84 return; |
85 return; |
85 } |
86 } |
86 ?> |
87 ?> |
|
88 |
87 <?php echo $args['before_widget']; ?> |
89 <?php echo $args['before_widget']; ?> |
|
90 |
88 <?php |
91 <?php |
89 if ( $title ) { |
92 if ( $title ) { |
90 echo $args['before_title'] . $title . $args['after_title']; |
93 echo $args['before_title'] . $title . $args['after_title']; |
91 } |
94 } |
|
95 |
|
96 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; |
|
97 |
|
98 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ |
|
99 $format = apply_filters( 'navigation_widgets_format', $format ); |
|
100 |
|
101 if ( 'html5' === $format ) { |
|
102 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. |
|
103 $title = trim( strip_tags( $title ) ); |
|
104 $aria_label = $title ? $title : $default_title; |
|
105 echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">'; |
|
106 } |
92 ?> |
107 ?> |
|
108 |
93 <ul> |
109 <ul> |
94 <?php foreach ( $r->posts as $recent_post ) : ?> |
110 <?php foreach ( $r->posts as $recent_post ) : ?> |
95 <?php |
111 <?php |
96 $post_title = get_the_title( $recent_post->ID ); |
112 $post_title = get_the_title( $recent_post->ID ); |
97 $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' ); |
113 $title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' ); |
|
114 $aria_current = ''; |
|
115 |
|
116 if ( get_queried_object_id() === $recent_post->ID ) { |
|
117 $aria_current = ' aria-current="page"'; |
|
118 } |
98 ?> |
119 ?> |
99 <li> |
120 <li> |
100 <a href="<?php the_permalink( $recent_post->ID ); ?>"><?php echo $title; ?></a> |
121 <a href="<?php the_permalink( $recent_post->ID ); ?>"<?php echo $aria_current; ?>><?php echo $title; ?></a> |
101 <?php if ( $show_date ) : ?> |
122 <?php if ( $show_date ) : ?> |
102 <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span> |
123 <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span> |
103 <?php endif; ?> |
124 <?php endif; ?> |
104 </li> |
125 </li> |
105 <?php endforeach; ?> |
126 <?php endforeach; ?> |
106 </ul> |
127 </ul> |
|
128 |
107 <?php |
129 <?php |
|
130 if ( 'html5' === $format ) { |
|
131 echo '</nav>'; |
|
132 } |
|
133 |
108 echo $args['after_widget']; |
134 echo $args['after_widget']; |
109 } |
135 } |
110 |
136 |
111 /** |
137 /** |
112 * Handles updating the settings for the current Recent Posts widget instance. |
138 * Handles updating the settings for the current Recent Posts widget instance. |
136 public function form( $instance ) { |
162 public function form( $instance ) { |
137 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
163 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
138 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; |
164 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; |
139 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; |
165 $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false; |
140 ?> |
166 ?> |
141 <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
167 <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> |
168 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
|
169 <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; ?>" /> |
|
170 </p> |
143 |
171 |
144 <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> |
172 <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> |
173 <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label> |
|
174 <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" /> |
|
175 </p> |
146 |
176 |
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' ); ?>" /> |
177 <p> |
148 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p> |
178 <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' ); ?>" /> |
|
179 <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label> |
|
180 </p> |
149 <?php |
181 <?php |
150 } |
182 } |
151 } |
183 } |