[wp-trac] [WordPress Trac] #28383: Adding have_the_post() as a shortcut to have_posts() and the_post() loops

WordPress Trac noreply at wordpress.org
Wed May 28 16:43:55 UTC 2014


#28383: Adding have_the_post() as a shortcut to have_posts() and the_post() loops
-------------------------+-----------------------------
 Reporter:  sc0ttkclark  |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Query        |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 In most examples and code usage, you'll see something like this:

 {{{
 <?php while ( have_posts() ) : the_post(); ?>
 ...templating here...
 <?php endwhile; ?>
 }}}

 What I'm recommending is a shortcode to make this easier, because how many
 use cases are there really for using have_posts() without the_post()?

 {{{
 <?php while ( have_the_post() ) : ?>
 ...templating here...
 <?php endwhile; ?>
 }}}

 The equivalent would be added to $wp_query->have_the_post() as well.

 Just let me know if there's interest in this and I'll whip up the quick
 patch that would essentially do this:

 {{{
 /**
  * Sets up the next post if the WordPress query has results to loop over.
  *
  * @see WP_Query::have_the_post()
  * @since 4.0
  * @uses $wp_query
  *
  * @return bool
  */
 function have_the_post() {

         global $wp_query;

         return $wp_query->have_the_post();

 }
 }}}

 And in WP_Query:

 {{{
 /**
  * Sets up the next post if the WordPress query has results to loop over.
  *
  * @since 4.0
  * @access public
  * @uses WP_Query::have_posts()
  * @uses WP_Query::the_post()

  * @return bool
  */
 function have_the_post() {

         if ( !$this->have_posts() ) {
                 return false;
         }

         $this->the_post();

         return true;

 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/28383>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list