LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » WordPress / WordPress Widgets » Add Recent/Latest/New Posts Widget In WordPress Without Any Plugin

Add Recent/Latest/New Posts Widget In WordPress Without Any Plugin

Add-Recent-Latest-New-Posts-Widget-In-WordPress-Without-Any-Plugin
Showing Recent content often facilitates your users to see them quickly specially on the sidebar of your single publish page. But also in some designing processes people wish to display recent posts in lots of different ways. This snippet will enable a custom, flexible and super easy recent posts, you can display it via widget. Allows you to display a list of the most recent posts title with thumbnail.

In this article, we usually are sharing the methods for you to display your recent written content in WordPress. Recent write-up widget would be the most crucial WordPress widget for any WordPress weblog. because it’s only WordPress widget where reader can read about your most up-to-date posts. So every single WordPress or even web developer wishes to make that beautiful and attractive.

You will find lots involving recent write-up widget pertaining to blogger on the net so you don’t have to make the newest one and a variety of them are very beautiful WordPress widgets in group of recent write-up. So that which you did we collected all of them and use it here with the one area so you don’t need to go some other place to get the other a single. WordPress widget for the latest post is quite indispensable pertaining to WordPress weblog. If you want clicks and traffic for the new post you then must try and make doodlekit widget involving recent write-up very appealing.

Table of Contents

Recommended For You:
Top 5 Best And Free WordPress SEO Plugins For Newbies And Pro

How To Install Recent/Latest/New Posts Widget In WordPress Without Any Plugin?

Step 1:) First of all, open Theme Functions (function.php) file from the list of your theme file in Dashboard -> Appearance -> Editor.
Step 2:) Copy all the below code and Paste it in the end of Theme Functions (function.php) file.

/* ------------------------------------------------------------------------- *
 * EXEWidgets: Recent/Latest/New Posts Widget
/* ------------------------------------------------------------------------- */
class Show_pres extends WP_Widget {
 public function __construct() {
 parent::__construct(
 'Show_pres', 
 __( 'EXEWidgets: Recent/Latest/New Posts' ), 
 array( 'description' => __( 'Show your recent/latest/new blog posts.' ), 
 ) 
 );
 $this->alt_option_name = 'Show_pres';
 add_action( 'save_post', array($this, 'flush_widget_cache') );
 add_action( 'deleted_post', array($this, 'flush_widget_cache') );
 add_action( 'switch_theme', array($this, 'flush_widget_cache') );
 }
 public function widget( $args, $instance ) {
 $cache = array();
 if ( ! $this->is_preview() ) {
 $cache = wp_cache_get( 'widget_pres', 'widget' );
 }
 if ( ! is_array( $cache ) ) {
 $cache = array();
 }
 if ( ! isset( $args['widget_id'] ) ) {
 $args['widget_id'] = $this->id;
 }
 if ( isset( $cache[ $args['widget_id'] ] ) ) {
 echo $cache[ $args['widget_id'] ];
 return;
 }
 ob_start();
 $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' ); /*If Title Field Is Empty */
 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
 $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5; /*If Post Count Field Is Empty */
 if ( ! $number ) {
 $number = 5;
 } 
 echo $args['before_widget'];
 echo '
 <style type="text/css">
 /* Recent/Latest/New Posts (Widget Style)
 ----------------------------------------------- */
 .recent_post {margin:0px;color:#333333;padding:5px;font-size:12px;background:#F2F2F2;border-bottom:1px solid #CCCCCC;border-top:1px solid #FFFFFF;}
 .recent_post:hover {background:#FAFAFA;}
 .recent_post .recent_post_image img {float:left;margin:0 5px 5px 0;border:0;-moz-border-radius:50%;-webkit-border-radius:50%;border-radius:50%;width:72px;height:72px;}
 .recent_post .recent_post_title {font-size:15px;line-height:20px;word-break:break-all;padding:15px 0;display:block;}
 </style>
 ';
 if ( $title ) {
 echo $args['before_title'] . $title . $args['after_title'];
 } 
 /* Recent Posts Original Code Start */
 $query = new WP_Query('post_status=publish&posts_per_page='.$number.''); 
 if( $query->have_posts() ) {
 while( $query->have_posts() ) {
 $query->the_post(); ?>

 <!-- Recent Posts -->
 <div class="recent_post">
 <a class="recent_post_image" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
 <?php if ( has_post_thumbnail() ): ?>
 <?php the_post_thumbnail('thumbnail'); ?>
 <?php else : ?>
 <img src="<?php echo get_template_directory_uri(); ?>/images/No-Thumbnail-Available.png" alt="<?php the_title(); ?>" />
 <?php endif; ?>
 </a>
 <span class="recent_post_title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></span>
 <div class="clear"></div>
 </div>
 <!-- Recent Posts -->

 <?php
 }
 wp_reset_postdata();
 }
 /* Recent Posts Original Code End */
 echo $args['after_widget']; 
 if ( ! $this->is_preview() ) {
 $cache[ $args['widget_id'] ] = ob_get_flush();
 wp_cache_set( 'widget_pres', $cache, 'widget' );
 } else {
 ob_end_flush();
 }
 }
 public function update( $new_instance, $old_instance ) {
 $instance = $old_instance;
 $instance['title'] = strip_tags( $new_instance['title'] );
 $instance['number'] = (int) $new_instance['number'];
 $this->flush_widget_cache();
 $alloptions = wp_cache_get( 'alloptions', 'options' );
 if ( isset( $alloptions['widget_rec_posts'] ) )
 delete_option( 'widget_rec_posts' );
 return $instance;
 }
 public function flush_widget_cache() {
 wp_cache_delete( 'widget_pres', 'widget' );
 }
 public function form( $instance ) {
 $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
 $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
 ?>
 <p>
 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 <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>
 <p>
 <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
 <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" />
 </p>
 <?php
 }
}
add_action( 'widgets_init', function () {
 register_widget( 'Show_pres' );
});

Step 3:) Save the file and move forward.

Recommended For You:
10 Of The Best WordPress Plugins For SEOs

EXEWidgets Recent Latest New Post Widget

How To Add Recent/Latest/New Posts Widget In WordPress?

Step 1:) No move to Dashboard -> Appearance -> Widgets and see EXEWidgets: Recent/Latest/New Posts under Available Widgets.
Step 2:) Now drag that widget to your sidebars widgets place or where ever you want.
Step 3:) Now add you desired title and no of posts count to show.
Step 4:) Save and now you are done.

Theme Files Editing Warning:

Keep in mind that every code that you will add in your themes files will be removed when you will change the theme. So after changing your theme, you have to follow this article again and you will get everything back same as it is now.

Last Words:

This is what we have and shared in easy steps for newbies so that they can easily know how it works. Stay with us because we are going to share a whole guide step by step about WordPress and make it easy for you. If you liked it then share it and be with us to get next tutorial. If you have any problem then feel free to ask us. We will help you with what we can or have.

You Like It, Please Share This Recipe With Your Friends Using...

4 Responses to “Add Recent/Latest/New Posts Widget In WordPress Without Any Plugin”

  1. sandybrown says:

    Good method for installment to your word press widget.

  2. Moh Shakeel says:

    Thanks Hassan Bro, Its Working Really For Me I Added To My Blog At:- http://www.teachtotech.com

Leave a Reply

Your email address will not be published. Required fields are marked *