Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.38 KB | None | 0 0
  1. add_action( 'widgets_init', 'switch_recent_posts_widget' );
  2.  
  3. function switch_recent_posts_widget() {
  4.  
  5.     unregister_widget( 'WP_Widget_Recent_Posts' );
  6.     register_widget( 'WP_Widget_Recent_Posts_Truncated' );
  7.  
  8. }
  9.  
  10. class WP_Widget_Recent_Posts_Truncated extends WP_Widget {
  11.    
  12.     function __construct() {
  13.         $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
  14.         parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
  15.         $this->alt_option_name = 'widget_recent_entries';
  16.  
  17.         add_action( 'save_post', array(&$this, 'flush_widget_cache') );
  18.         add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
  19.         add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
  20.     }
  21.  
  22.     function widget($args, $instance) {
  23.         $cache = wp_cache_get('widget_recent_posts', 'widget');
  24.  
  25.         if ( !is_array($cache) )
  26.             $cache = array();
  27.  
  28.         if ( isset($cache[$args['widget_id']]) ) {
  29.             echo $cache[$args['widget_id']];
  30.             return;
  31.         }
  32.  
  33.         ob_start();
  34.         extract($args);
  35.  
  36.         $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
  37.         if ( ! $number = absint( $instance['number'] ) )
  38.             $number = 10;
  39.  
  40.         $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true));
  41.         if ($r->have_posts()) :
  42. ?>
  43.         <?php echo $before_widget; global $post ?>
  44.         <?php if ( $title ) echo $before_title . $title . $after_title; ?>
  45.         <ul>
  46.         <?php  while ($r->have_posts()) : $r->the_post(); ?>
  47.         <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
  48.             <?php
  49.             if( get_the_title() )
  50.                 the_title(); // DO YOUR STRING LENGTH RESTRICTION HERE!
  51.             else
  52.                 the_ID();
  53.             ?>
  54.         </a></li>
  55.         <?php endwhile; ?>
  56.         </ul>
  57.         <?php echo $after_widget; ?>
  58. <?php
  59.         // Reset the global $the_post as this query will have stomped on it
  60.         wp_reset_postdata();
  61.  
  62.         endif;
  63.  
  64.         $cache[$args['widget_id']] = ob_get_flush();
  65.         wp_cache_set('widget_recent_posts', $cache, 'widget');
  66.     }
  67.  
  68.     function update( $new_instance, $old_instance ) {
  69.         $instance = $old_instance;
  70.         $instance['title'] = strip_tags($new_instance['title']);
  71.         $instance['number'] = (int) $new_instance['number'];
  72.         $this->flush_widget_cache();
  73.  
  74.         $alloptions = wp_cache_get( 'alloptions', 'options' );
  75.         if ( isset($alloptions['widget_recent_entries']) )
  76.             delete_option('widget_recent_entries');
  77.  
  78.         return $instance;
  79.     }
  80.  
  81.     function flush_widget_cache() {
  82.         wp_cache_delete('widget_recent_posts', 'widget');
  83.     }
  84.  
  85.     function form( $instance ) {
  86.         $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  87.         $number = isset($instance['number']) ? absint($instance['number']) : 5;
  88. ?>
  89.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  90.         <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>
  91.  
  92.         <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
  93.         <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>
  94. <?php
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement