Skip to content

Instantly share code, notes, and snippets.

@KustomDeveloper
Last active December 6, 2017 01:36
Show Gist options
  • Save KustomDeveloper/025bfd18cf6c4ca24fb2b773151486d5 to your computer and use it in GitHub Desktop.
Save KustomDeveloper/025bfd18cf6c4ca24fb2b773151486d5 to your computer and use it in GitHub Desktop.
/**
*
* Recent Posts Widget
* Note* - The WP_Widget class is located in wp-includes/class-wp-widget.php.
*/
//Add Custom Excerpt Length
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt) >= $limit) {
array_pop($excerpt);
$excerpt = implode(" ", $excerpt) . '...';
} else {
$excerpt = implode(" ", $excerpt);
}
$excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);
return $excerpt;
//example use: echo excerpt(25);
}
//Create Recent Posts Widget Class
class Recent_Posts_Widget extends WP_Widget {
/**
* Register Widget With Wordpress
*/
public function __construct() {
parent::__construct(
'recent_posts_widget', // Base ID
__('Recent Posts Widget', 'text_domain'), // Widget name
array( 'description' => __( 'Recent Posts Widget', 'text_domain' ), )
);
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
// if text contain a value, save it to $num_posts
if ( isset( $instance[ 'num_posts' ] ) ) {
$num_posts = $instance[ 'num_posts' ];
}
// if title contain a value, save it to $title
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
// else set the $title to string 'Recent Posts'
$title = __( 'Recent Posts', 'text_domain' );
}
?>
<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 esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'num_posts' ); ?>"><?php _e( 'Number of Posts:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'num_posts' ); ?>" name="<?php echo $this->get_field_name( 'num_posts' ); ?>" type="text" value="<?php echo esc_attr( $num_posts ); ?>">
</p>
<?php
}
// array $instance Stores values from database.
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . '<h3 class=">' . 'rp-banner-title">' . $title . '</h3>' . $args['after_title']; ?>
<?php //Place Recent Posts Function Here ?>
<style>#gr-recent-posts {position:relative;width:100%;max-width:300px;margin: 0 0 30px 0;padding: 10px 20px 10px 0;}#gr-recent-posts p {margin: 0 0 20px 0;font-size: 12px;}.recent-post-thumbnail {float: left;padding:0;margin: 0 6px 15px 0;}.wp-post-image {width: 40px;height: 40px;border: 3px solid #555;}#gr-recent-posts a{box-shadow:none !important;text-decoration:none !important;}#gr-recent-posts a:hover{box-shadow:none !important;text-decoration:none !important;}.rp-title{float:left;}.rp-excerpt{font-size:13px;margin:0 0 10px 47px;}</style>
<?php $rp_post_count = $instance['num_posts']; ?>
<div id="gr-recent-posts">
<?php $custom_query = new WP_Query('posts_per_page=' . $rp_post_count); // exclude category 9
while($custom_query->have_posts()) : $custom_query->the_post(); ?>
<div class="recent-post-thumbnail">
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?></a>
<?php } else { ?>
<a href="<?php the_permalink(); ?>"> <img src="http://via.placeholder.com/40x40" /></a>
<?php } ?>
</div>
<a href="<?php the_permalink(); ?>">
<span class="rp-title"><?php the_title(); ?></span></a><br/>
<div class="rp-excerpt"><?php echo excerpt(15); ?></div>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>
</div>
<!-- End Recent Posts -->
<?php echo $args['after_widget']; }
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['num_posts'] = ( ! empty( $new_instance['num_posts'] ) ) ? strip_tags( $new_instance['num_posts'] ) : '';
return $instance;
}
} // Recent Posts Widget Class
add_action( 'widgets_init', function() {
register_widget( 'Recent_Posts_Widget' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment