Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Recent Posts Widget

class GenerateWP_Recent_Posts extends WP_Widget {

	public function __construct() {

		parent::__construct(
			'generatewp_recent_posts',
			__( 'Recent Posts', 'generatewp' ),
			array(
				'description' => __( 'Most recent posts on site.', 'generatewp' ),
				'classname'   => 'widget_recent_entries',
			)
		);

	}

	public function widget( $args, $instance ) {

		$title  = ( ! empty( $instance['generatewp_title']  ) ) ? $instance['generatewp_title'] : __( 'Recent Posts' );
		$number = ( ! empty( $instance['generatewp_number'] ) ) ? absint( $instance['generatewp_number'] ) : 5;
		
		if ( ! $number )
			$number = 5;
		
		// Before widget tag
		echo $args['before_widget'];
		
		// Title
		if ( ! empty( $title ) ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}
		
		// Recent Posts
		$query = new WP_Query( array (
			'posts_per_page'      => $number,
			'ignore_sticky_posts' => true
		) );
		if ( $query->have_posts() ) :
		
			echo '<ul>';
			while ( $query->have_posts() ) : $query->the_post();
				$post_title = ( get_the_title() ? get_the_title() : get_the_ID() );
				echo '<li><a href="' . get_permalink() . '">' . $post_title . '</a></li>';
			endwhile;
			echo '</ul>';
		
		endif;
		wp_reset_postdata();
		
		// After widget tag
		echo $args['after_widget'];

	}

	public function form( $instance ) {

		// Set default values
		$instance = wp_parse_args( (array) $instance, array( 
			'generatewp_title' => '',
			'generatewp_number' => '5',
		) );

		// Retrieve an existing value from the database
		$generatewp_title = !empty( $instance['generatewp_title'] ) ? $instance['generatewp_title'] : '';
		$generatewp_number = !empty( $instance['generatewp_number'] ) ? $instance['generatewp_number'] : '';

		// Form fields
		echo '<p>';
		echo '	<label for="' . $this->get_field_id( 'generatewp_title' ) . '" class="generatewp_title_label">' . __( 'Title', 'generatewp' ) . '</label>';
		echo '	<input type="text" id="' . $this->get_field_id( 'generatewp_title' ) . '" name="' . $this->get_field_name( 'generatewp_title' ) . '" class="widefat" placeholder="' . esc_attr__( '', 'generatewp' ) . '" value="' . esc_attr( $generatewp_title ) . '">';
		echo '</p>';

		echo '<p>';
		echo '	<label for="' . $this->get_field_id( 'generatewp_number' ) . '" class="generatewp_number_label">' . __( 'Posts to show', 'generatewp' ) . '</label>';
		echo '	<input type="number" id="' . $this->get_field_id( 'generatewp_number' ) . '" name="' . $this->get_field_name( 'generatewp_number' ) . '" class="widefat" placeholder="' . esc_attr__( '', 'generatewp' ) . '" value="' . esc_attr( $generatewp_number ) . '">';
		echo '	<span class="description">' . __( 'Number of posts to show.', 'generatewp' ) . '</span>';
		echo '</p>';

	}

	public function update( $new_instance, $old_instance ) {

		$instance = $old_instance;

		$instance['generatewp_title'] = !empty( $new_instance['generatewp_title'] ) ? strip_tags( $new_instance['generatewp_title'] ) : '';
		$instance['generatewp_number'] = !empty( $new_instance['generatewp_number'] ) ? strip_tags( $new_instance['generatewp_number'] ) : '';

		return $instance;

	}

}

function generatewp_register_widgets() {
	register_widget( 'GenerateWP_Recent_Posts' );
}
add_action( 'widgets_init', 'generatewp_register_widgets' );