0

I'm getting a really weird bug. I'm getting the same comments from 1 specific post which is the "Hello world" for every single post! But when I remove a bit of code for a recent posts widget that I'm trying to create, this bug disappears... here's the code of the recent posts widget:

class Hype_Recent_Posts extends WP_Widget {

    public function __construct() {
        parent::__construct(
            'hype_rp', // Base ID
            __('Paradox Recent Posts', 'hype'), // Name
            array( 'description' => __( 'Display your recent posts, with a Thumbnail.', 'hype' ), ) // Args
        );
    }

    public function widget( $args, $instance ) {
        $title = apply_filters( 'widget_title', $instance['title'] );
        $no_of_posts = apply_filters( 'no_of_posts', $instance['no_of_posts'] );

        echo $args['before_widget'];

        if ( ! empty( $title ) )
            echo $args['before_title'] . $title . $args['after_title'];

        // WP_Query arguments
        $qa = array (
            'post_type'              => 'post',
            'posts_per_page'         => 5,
            'offset'                 => 0,
            'ignore_sticky_posts'    => 1

        );

        // The Query
        $recent_articles_wtb = new WP_Query( $qa );
        if($recent_articles_wtb->have_posts()) : ?>
        <ul class="rp">
        <?php
            while($recent_articles_wtb->have_posts()) : 
            $recent_articles_wtb->the_post();
         ?>

                 <li class='rp-item'>
                 <?php if( has_post_thumbnail() ) : ?>
                 <div class='rp-thumb'><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?><i class="fa fa-folder-open"></i></a></div>
                 <?php else : ?>
                 <div class='rp-thumb'><a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/images/9viqf41.jpg"><i class="fa fa-folder-open"></i></a></div>
                 <?php endif; ?>    
                 <div class='rp-title'><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
                 </li>

        <?php
              endwhile;
           else: 
        ?>

              Oops, there are no posts.

        <?php
           endif;
        ?>
        </ul>
        <?php

        echo $args['after_widget'];
    }

    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'Latest Articles', 'hype' );
        }
        if ( isset( $instance[ 'no_of_posts' ] ) ) {
            $no_of_posts = $instance[ 'no_of_posts' ];
        }
        else {
            $no_of_posts = __( '5', 'hype' );
        }
        ?>
        <p>
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:','hype' ); ?></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 ); ?>" />

        <label for="<?php echo $this->get_field_id( 'no_of_posts' ); ?>"><?php _e( 'No. of Posts:', 'hype' ); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'no_of_posts' ); ?>" name="<?php echo $this->get_field_name( 'no_of_posts' ); ?>" type="text" value="<?php echo esc_attr( $no_of_posts ); ?>" />
        </p>
        <?php 
    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['no_of_posts'] = ( ! empty( $new_instance['no_of_posts'] ) ) ? strip_tags( $new_instance['no_of_posts'] ) : '5';
        if ( is_numeric($new_instance['no_of_posts']) == false ) {
            $instance['no_of_posts'] = $old_instance['no_of_posts'];
            }
        return $instance;


    }
}
add_action( 'widgets_init', 'register_hype_widget' );  
function register_hype_widget() {  
    register_widget( 'Hype_Recent_Posts' );  
}  
1

You are stomping on the global wordpress posts with your plugin.

Doing your wp_query call and then calling

$recent_articles_wtb->the_post();

means that later (after your loop), you will want to call

wp_reset_postdata(); # Thanks Milo

That should reset the global $current_post and $post objects.

  • 2
    I think you want wp_reset_postdata(), not rewind_posts(). – Milo Feb 19 '15 at 2:05
  • Milo's answer did the job, I tried the rewind_posts(); but it was taking too long to load the page, so I tried wp_reset_postdata(); suggested by @Milo and this problem disappeared. Thanks a lot guys! – Chun Feb 19 '15 at 12:11
  • Thanks Milo. Updating. I never do things that way so I occasionally forget which is which. – Privateer Feb 19 '15 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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