Skip to content

Instantly share code, notes, and snippets.

@markandcurry
Forked from paulruescher/Extend Recent Posts
Last active September 21, 2017 15:46
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 markandcurry/62dc797d6664051e82e5bebed44eda37 to your computer and use it in GitHub Desktop.
Save markandcurry/62dc797d6664051e82e5bebed44eda37 to your computer and use it in GitHub Desktop.
Used this to change the output of WordPress' Recent Posts Widget
/**
* Extend Recent Posts Widget
*
* Adds different formatting to the default WordPress Recent Posts Widget
*/
Class My_Recent_Posts_Widget extends WP_Widget_Recent_Posts {
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
if( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
$number = 10;
$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() ) :
echo $before_widget;
if( $title ) echo $before_title . $title . $after_title; ?>
<ul>
<?php while( $r->have_posts() ) : $r->the_post(); ?>
<li><h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
<p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="read-more">READ MORE</a></li>
<?php endwhile; ?>
</ul>
<?php
echo $after_widget;
wp_reset_postdata();
endif;
}
}
function my_recent_widget_registration() {
unregister_widget('WP_Widget_Recent_Posts');
register_widget('My_Recent_Posts_Widget');
}
add_action('widgets_init', 'my_recent_widget_registration');
@markandcurry
Copy link
Author

This custom function adds an excerpt section for the recent posts section of the sidebar. Add this to the functions file of your child theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment