Guest User

Untitled

a guest
Nov 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.28 KB | None | 0 0
  1. <?php
  2.  
  3. /* Reduced Excerptsfunction
  4. ---------------------------------------------------------------------- */
  5.  
  6. function my_excerpt($limit) {
  7.     $excerpt = explode(' ', get_the_excerpt(), $limit);
  8.     if (count($excerpt)>=$limit) {
  9.         array_pop($excerpt);
  10.         $excerpt = implode(" ",$excerpt).'...';
  11.     } else {
  12.         $excerpt = implode(" ",$excerpt).'...';
  13.     }  
  14.     $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  15.     return $excerpt;
  16. }
  17.  
  18.  
  19.  
  20.  
  21. /**
  22.  * MyRecentPostsWidget Class
  23.  */
  24. class MyRecentPostsWidget extends WP_Widget {
  25.     /** constructor */
  26.     function MyRecentPostsWidget() {
  27.         parent::WP_Widget( 'myrecentposts', $name = 'Custom Recent Posts' );
  28.     }
  29.  
  30.     /** @see WP_Widget::widget */
  31.     function widget( $args, $instance ) {
  32.  
  33.         global $before_title, $after_title, $before_widget, $after_widget;
  34.         $title = $instance['title'];
  35.         $showposts = $instance['showposts'];
  36.  
  37.         $args = array(
  38.             'showposts' => $showposts
  39.         );
  40.            
  41.         global $post;
  42.         $tmp_post = $post;
  43.         $lastestposts = get_posts( $args );
  44.        
  45.         if ( $lastestposts ) :
  46.        
  47.             echo '<li id="recent-posts-widget" class="widgetcontainer">';
  48.            
  49.             if ( $title )
  50.                 echo '<h3 class="widget-title">' . $title . '</h3>'; ?>
  51.  
  52.             <ul>
  53.                 <?php
  54.                 foreach( $lastestposts as $post ) :
  55.                     setup_postdata( $post );
  56.                    
  57.                     $attr = array(
  58.                         'title' => $post->post_title
  59.                     ); ?>
  60.                    
  61.                     <li class="clearfix">
  62.                         <div>
  63.                             <a class="widget-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  64.                             <?php
  65.                             if ( $my_excerpt = get_post_meta($post->ID, 'excerpt_value', true) ) {
  66.                                 echo $my_excerpt;
  67.                             } else {
  68.                                 echo my_excerpt( 10 );
  69.                             }
  70.                             ?>
  71.                             <a class="more-link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Leer m&aacute;s &raquo;</a>
  72.                         </div>
  73.                     </li>
  74.                    
  75.                 <?php
  76.                 endforeach;
  77.                 $post = $tmp_post;
  78.                 //wp_reset_query();
  79.                 ?>
  80.             </ul>
  81.        
  82.             <?
  83.             echo '</li>';
  84.         endif;
  85.        
  86.     }
  87.  
  88.     /** @see WP_Widget::update */
  89.     function update( $new_instance, $old_instance ) {
  90.         $instance = $old_instance;
  91.         $instance['title'] = strip_tags($new_instance['title']);
  92.         $instance['showposts'] = strip_tags($new_instance['showposts']);
  93.         return $instance;
  94.     }
  95.  
  96.     /** @see WP_Widget::form */
  97.     function form( $instance ) {
  98.         if ( $instance ) {
  99.             $title = esc_attr( $instance['title'] );
  100.       $showposts = esc_attr( $instance['showposts'] );
  101.         }
  102.         else {
  103.             $title = __( 'Recent Posts' );
  104.             $showposts = 3;
  105.         }
  106.         ?>
  107.         <p>
  108.             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title:'); ?></label>
  109.             <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; ?>" />
  110.         </p>
  111.         <p>
  112.             <label for="<?php echo $this->get_field_id('showposts'); ?>"><?php _e('Number of Recent Posts:'); ?></label>
  113.             <input class="widefat" id="<?php echo $this->get_field_id('showposts'); ?>" name="<?php echo $this->get_field_name('showposts'); ?>" type="text" value="<?php echo $showposts; ?>" />
  114.         </p>
  115.         <?php
  116.     }
  117.  
  118. } // class MyRecentPostsWidget
  119.  
  120. // register MyRecentPostsWidget
  121. add_action( 'widgets_init', create_function( '', 'return register_widget("MyRecentPostsWidget");' ) );
  122.  
  123. ?>
Add Comment
Please, Sign In to add comment