Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Reduced Excerptsfunction
- ---------------------------------------------------------------------- */
- function my_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;
- }
- /**
- * MyRecentPostsWidget Class
- */
- class MyRecentPostsWidget extends WP_Widget {
- /** constructor */
- function MyRecentPostsWidget() {
- parent::WP_Widget( 'myrecentposts', $name = 'Custom Recent Posts' );
- }
- /** @see WP_Widget::widget */
- function widget( $args, $instance ) {
- global $before_title, $after_title, $before_widget, $after_widget;
- $title = $instance['title'];
- $showposts = $instance['showposts'];
- $args = array(
- 'showposts' => $showposts
- );
- global $post;
- $tmp_post = $post;
- $lastestposts = get_posts( $args );
- if ( $lastestposts ) :
- echo '<li id="recent-posts-widget" class="widgetcontainer">';
- if ( $title )
- echo '<h3 class="widget-title">' . $title . '</h3>'; ?>
- <ul>
- <?php
- foreach( $lastestposts as $post ) :
- setup_postdata( $post );
- $attr = array(
- 'title' => $post->post_title
- ); ?>
- <li class="clearfix">
- <div>
- <a class="widget-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
- <?php
- if ( $my_excerpt = get_post_meta($post->ID, 'excerpt_value', true) ) {
- echo $my_excerpt;
- } else {
- echo my_excerpt( 10 );
- }
- ?>
- <a class="more-link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Leer más »</a>
- </div>
- </li>
- <?php
- endforeach;
- $post = $tmp_post;
- //wp_reset_query();
- ?>
- </ul>
- <?
- echo '</li>';
- endif;
- }
- /** @see WP_Widget::update */
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance['title'] = strip_tags($new_instance['title']);
- $instance['showposts'] = strip_tags($new_instance['showposts']);
- return $instance;
- }
- /** @see WP_Widget::form */
- function form( $instance ) {
- if ( $instance ) {
- $title = esc_attr( $instance['title'] );
- $showposts = esc_attr( $instance['showposts'] );
- }
- else {
- $title = __( 'Recent Posts' );
- $showposts = 3;
- }
- ?>
- <p>
- <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget 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('showposts'); ?>"><?php _e('Number of Recent Posts:'); ?></label>
- <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; ?>" />
- </p>
- <?php
- }
- } // class MyRecentPostsWidget
- // register MyRecentPostsWidget
- add_action( 'widgets_init', create_function( '', 'return register_widget("MyRecentPostsWidget");' ) );
- ?>
Add Comment
Please, Sign In to add comment