Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Display Recent Posts Widget in WordPress

Showcasing your recently published content is one of the most simple ways of improving almost every website performance metrics.

The good thing to know is that you can display your recently published content in high-traffic areas such as sidebars, after the posts and the footer. This makes sure that you give maximum visibility to your content and get all the benefits of publishing content. In this article, I will show you how you can create a recent post Plugin that simplifies the process of showcasing the recent posts on your website.

Register the Plugin

I will start by registering the plugin on the website. Once registered, the plugin will show up in the Plugins section of the website.

/*
Plugin Name: Recent Posts widget extended
Description: This plugin creates a widget for displaying recent posts on the front end.
Version: 1.X
Author: WordPress Recent Posts Widget

*/
class RecentPostsWithExcerpts extends WP_Widget {
function __construct() {
$widget_ops = array('classname' => 'recent_with_excerpt', 'description' => __( 'Your most recent posts, with optional excerpts', 'recent_posts_with_excerpts') );
parent::__construct('RecentPostsWithExcerpts', __('Recent Posts with Excerpts', 'recent_posts_with_excerpts'), $widget_ops);
}

In the code snippet above, RecentPostsWithExprects() function registers the plugin. As a result, this is how the plugin entry would appear in the Plugin list:

The Code of the Widget

Here is the code snippet that carries out multiple actions. First, it creates the Widget that would appear on the frontend. Next, the snippet retrieves the latest published blogs. Once this is done, the excerpts (both the built-in ones and the Advanced Excerpts) of the blogs are displayed through a loop (see the comment within the snippet). Once this is done, the function wp_reset_query() restores the $wp_query and the global post data of the original main query.

function widget( $args, $instance ) {
    	global $before_widget,$instance;
    	extract( $args );
    	$title = apply_filters('widget_title', $instance['title']);
    	echo $before_widget,$title;
    	$ul_classes = 'recent_posts_with_excerpts';
    	$ul_classes = apply_filters('recent_posts_with_excerpts_list_classes', $ul_classes);
    	if ( !empty( $ul_classes ) )
        	$ul_classes = ' class="'.$ul_classes.'"';
    	$li_classes = '';
    	$li_classes = apply_filters('recent_posts_with_excerpts_item_classes', $li_classes);
    	if ( !empty( $li_classes ) )
        	$li_classes = ' class="'.$li_classes.'"';
    	$h2_classes = 'recent_posts_with_excerpts';
    	$h2_classes = apply_filters('recent_posts_with_excerpts_heading_classes', $h2_classes);
    	if ( !empty( $h2_classes ) )
        	$h2_classes = ' class="'.$h2_classes.'"';
        do_action('recent_posts_with_excerpts_begin');
    	echo '
    '; // retrieve last n blog posts $q = array('posts_per_page' => $instance['numposts']); if (!empty($instance['tag'])) $q['tag'] = $instance['tag']; $q = apply_filters('recent_posts_with_excerpts_query', $q, $instance); $rpwe = new wp_query($q); // the Loop if ($rpwe->have_posts()) : while ($rpwe->have_posts()) : $rpwe->the_post(); echo '
  • '; echo '

    '.get_the_title().'

    '; if (!empty($date)) echo '

    '.get_the_time($date).'

    '; { // show the excerpt ?>

    ">

The Form for the Widget Placement

The next snippet generates the form that the user will see when placing the widget on the front end. This snippet creates a simple form that the user can fill out to place the widget on the website.

function form( $instance ) {
	if (get_option('show_on_front') == 'page')
    	$link = get_permalink(get_option('page_for_posts'));
	else
    	$link = home_url();
	//Defaults
	$instance = wp_parse_args( (array) $instance, array(
    	'title' => __('Recent Posts', 'recent_posts_with_excerpts'),
    	'numposts' => 5,
    	'numexcerpts' => 5,
    	'date' => get_option('date_format'),
    	'more_text' => __('Read More', 'recent_posts_with_excerpts'),
    	'words' => '25',
    	'tag' => '',
	));
	?>



Here’s how the widget’s form would look like in action:

How to Set up and Use the Recent Post plugin

First, create folder having name “recent-post-widget-extended”. In this folder create file having name “recent-post-widget-extended.php”. Activate plugin than it shows in widget area as shown in fig.

To set up and get the plugin operational, create a folder named recent-post-widget-extended in the WordPress Plugin folder. In this folder, create a file and name it recent-post-widget-extended.php. Paste the code of the plugin in this file and save it.

Next, activate the plugin and you would be able to see it on the Plugin section of the WordPress Dashboard.

When you are ready, you can insert the widget in any location on the frontend of your WordPress websites

Once deployed, here is how the widget would look on the frontend:

The Full Code of the Plugin

 'recent_with_excerpt', 'description' => __( 'Your most recent posts, with optional excerpts', 'recent_posts_with_excerpts') );
parent::__construct('RecentPostsWithExcerpts', __('Recent Posts with Excerpts', 'recent_posts_with_excerpts'), $widget_ops);
}
function widget( $args, $instance ) {
global $before_widget,$intance;
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget,$title;
$ul_classes = 'recent_posts_with_excerpts';
$ul_classes = apply_filters('recent_posts_with_excerpts_list_classes', $ul_classes);
if ( !empty( $ul_classes ) )
$ul_classes = ' class="'.$ul_classes.'"';
$li_classes = '';
$li_classes = apply_filters('recent_posts_with_excerpts_item_classes', $li_classes);
if ( !empty( $li_classes ) )
$li_classes = ' class="'.$li_classes.'"';
$h2_classes = 'recent_posts_with_excerpts';
$h2_classes = apply_filters('recent_posts_with_excerpts_heading_classes', $h2_classes);
if ( !empty( $h2_classes ) )
$h2_classes = ' class="'.$h2_classes.'"';
do_action('recent_posts_with_excerpts_begin');
echo '
    '; // retrieve last n blog posts $q = array('posts_per_page' => $instance['numposts']); if (!empty($instance['tag'])) $q['tag'] = $instance['tag']; $q = apply_filters('recent_posts_with_excerpts_query', $q, $intance); $rpwe = new wp_query($q); // the Loop if ($rpwe->have_posts()) : while ($rpwe->have_posts()) : $rpwe->the_post(); echo '
  • '; echo '

    '.get_the_title().'

    '; if (!empty($date)) echo '

    '.get_the_time($date).'

    '; { // show the excerpt ?>

    ">

__('Recent Posts', 'recent_posts_with_excerpts'), 'numposts' => 5, 'numexcerpts' => 5, 'date' => get_option('date_format'), 'more_text' => __('Read More', 'recent_posts_with_excerpts'), 'words' => '25', 'tag' => '', )); ?>



Conclusion

This plugin is a great way of showcasing your recently published content on the website frontend. When placed on the location of your choice, the recent posts would attract readers into further exploring your website. All you need to do is to fil out a simple form that includes the title of the post, the number of posts to be visible in the widget and the excerpt from the post.

If you need help with this plugin, Do leave a comment and I will get back to you.

The post How to Display Recent Posts Widget in WordPress appeared first on WPblog.



This post first appeared on WordPress Tutorials And Guides, please read the originial post: here

Share the post

How to Display Recent Posts Widget in WordPress

×

Subscribe to Wordpress Tutorials And Guides

Get updates delivered right to your inbox!

Thank you for your subscription

×