Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Created February 5, 2020 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/6f49698e4e0b10fa975be925152b0f8c to your computer and use it in GitHub Desktop.
Save nfsarmento/6f49698e4e0b10fa975be925152b0f8c to your computer and use it in GitHub Desktop.
Extend Recent Posts Widget with Thumbnail Image and others
/**
* Extend Recent Posts Widget
*
* Adds different formatting (images) to the default WordPress Recent Posts Widget
*/
Class Ns_Recent_Posts_Widget extends WP_Widget_Recent_Posts {
function widget($args, $instance) {
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
if ( ! $number )
$number = 5;
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
/**
* Filter the arguments for the Recent Posts widget.
*
* @since 3.4.0
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the recent posts.
*/
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'ignore_sticky_posts' => true
) ) );
if ($r->have_posts()) :
?>
<?php echo $args['before_widget']; ?>
<?php if ( $title ) {
echo $args['before_title'] . esc_attr( $title ) . $args['after_title'];
} ?>
<ul>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<li>
<?php the_post_thumbnail(); ?>
<a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
<?php if ( $show_date ) : ?>
<span class="post-date"><?php echo get_the_date(); ?></span>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget']; ?>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
}
}
function ns_recent_widget_registration() {
unregister_widget('WP_Widget_Recent_Posts');
register_widget('Ns_Recent_Posts_Widget');
}
add_action('widgets_init', 'ns_recent_widget_registration');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment