4

Hi i'm using undersocres to create a theme. I need to add post thumbnails to the recent post widget, but I'm stuck. I try the code below, but I get a duplicated recent post widget, one without thumbnails images and the other with thumbnails images.

I understand that this is because I'm not modify the original recent post widget, instead I add another. My question is: How can I modify the wordpress core recent post to show post-thumbnails before my title. This is what I'm try resulting in a duplicated recent post widget.

<div id="secondary" class="widget-area" role="complementary">
        <?php dynamic_sidebar( 'sidebar-1' ); ?>

        <?php $recent_posts = wp_get_recent_posts();
        foreach( $recent_posts as $recent ){
            if($recent['post_status']=="publish"){
                if ( has_post_thumbnail($recent["ID"])) {
                        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .   get_the_post_thumbnail($recent["ID"], 'thumbnail'). $recent["post_title"].'</a></li> ';
                }else{
                        echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a></li> ';
                }
             }
        }
        ?>
</div><!-- #secondary -->
3
  • You can see how to extend the current widget here - gist.github.com/paulruescher/2998060 . Dec 30, 2015 at 15:41
  • Thanks but I'm trying to do this with out plugins Dec 30, 2015 at 15:47
  • Technically this is not a exactly a plugin but what you want to do is a bit of plugin territory. It was just an illustrative way on how you can extend the default widget. On the other hand if you have duplicate posts using your code and the "recent" widget you can try de-registering it (check lines 37-41, omit line 39). Dec 30, 2015 at 15:52

1 Answer 1

8

HERE IS THE SOLUTION

/**
 * Extend Recent Posts Widget 
 *
 * Adds different formatting to the default WordPress Recent Posts Widget
 */

Class My_Recent_Posts_Widget extends WP_Widget_Recent_Posts {

        function widget($args, $instance) {

                if ( ! isset( $args['widget_id'] ) ) {
                $args['widget_id'] = $this->id;
            }

            $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts' );

            /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
            $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

            $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
            if ( ! $number )
                $number = 5;
            $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;

            /**
             * Filter the arguments for the Recent Posts widget.
             *
             * @since 3.4.0
             *
             * @see WP_Query::get_posts()
             *
             * @param array $args An array of arguments used to retrieve the recent posts.
             */
            $r = new WP_Query( apply_filters( 'widget_posts_args', array(
                'posts_per_page'      => $number,
                'no_found_rows'       => true,
                'post_status'         => 'publish',
                'ignore_sticky_posts' => true
            ) ) );

            if ($r->have_posts()) :
            ?>
            <?php echo $args['before_widget']; ?>
            <?php if ( $title ) {
                echo $args['before_title'] . $title . $args['after_title'];
            } ?>
            <ul>
            <?php while ( $r->have_posts() ) : $r->the_post(); ?>
                <li>
                    <?php the_post_thumbnail(); ?>
                    <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
                <?php if ( $show_date ) : ?>
                    <span class="post-date"><?php echo get_the_date(); ?></span>
                <?php endif; ?>
                </li>
            <?php endwhile; ?>
            </ul>
            <?php echo $args['after_widget']; ?>
            <?php
            // Reset the global $the_post as this query will have stomped on it
            wp_reset_postdata();

            endif;
        }
}
function my_recent_widget_registration() {
  unregister_widget('WP_Widget_Recent_Posts');
  register_widget('My_Recent_Posts_Widget');
}
add_action('widgets_init', 'my_recent_widget_registration');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.