Make WordPress Core

Changeset 41890


Ignore:
Timestamp:
10/18/2017 12:34:01 AM (6 years ago)
Author:
westonruter
Message:

Widgets: Use a foreach loop instead of The Loop to iterate over posts in Recent Posts widget to avoid needing to reset a polluted global $post.

Props welcher, westonruter.
Amends [14607].
See #12320.
Fixes #37312.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets/class-wp-widget-recent-posts.php

    r41685 r41890  
    5252
    5353        $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
    54         if ( ! $number )
     54        if ( ! $number ) {
    5555            $number = 5;
     56        }
    5657        $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
    5758
     
    7172            'no_found_rows'       => true,
    7273            'post_status'         => 'publish',
    73             'ignore_sticky_posts' => true
     74            'ignore_sticky_posts' => true,
    7475        ), $instance ) );
    7576
    76         if ($r->have_posts()) :
     77        if ( ! $r->have_posts() ) {
     78            return;
     79        }
    7780        ?>
    7881        <?php echo $args['before_widget']; ?>
    79         <?php if ( $title ) {
     82        <?php
     83        if ( $title ) {
    8084            echo $args['before_title'] . $title . $args['after_title'];
    81         } ?>
     85        }
     86        ?>
    8287        <ul>
    83         <?php while ( $r->have_posts() ) : $r->the_post(); ?>
    84             <li>
    85                 <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : _e( '(no title)' ); ?></a>
    86             <?php if ( $show_date ) : ?>
    87                 <span class="post-date"><?php echo get_the_date(); ?></span>
    88             <?php endif; ?>
    89             </li>
    90         <?php endwhile; ?>
     88            <?php foreach ( $r->posts as $recent_post ) : ?>
     89                <?php
     90                $post_title = get_the_title( $recent_post->ID );
     91                $title      = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
     92                ?>
     93                <li>
     94                    <a href="<?php the_permalink( $recent_post->ID ); ?>"><?php echo $title ; ?></a>
     95                    <?php if ( $show_date ) : ?>
     96                        <span class="post-date"><?php echo get_the_date( '', $recent_post->ID ); ?></span>
     97                    <?php endif; ?>
     98                </li>
     99            <?php endforeach; ?>
    91100        </ul>
    92         <?php echo $args['after_widget']; ?>
    93101        <?php
    94         // Reset the global $the_post as this query will have stomped on it
    95         wp_reset_postdata();
    96 
    97         endif;
     102        echo $args['after_widget'];
    98103    }
    99104
Note: See TracChangeset for help on using the changeset viewer.