Opened 10 years ago
Closed 9 years ago
#28383 closed enhancement (maybelater)
Adding have_the_post() as a shortcut to have_posts() and the_post() loops
Reported by: | sc0ttkclark | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Query | Keywords: | has-patch close |
Focuses: | template | Cc: |
Description
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; }
Change History (12)
#3
in reply to:
↑ description
@
10 years ago
- Focuses template removed
Replying to sc0ttkclark:
because how many use cases are there really for using have_posts() without the_post()?
I wanted to clarify this point further, not saying that this is designed to replace all usage of have_posts(), but all usage of have_posts() that also uses the_post() right after have_posts(), specifically in while() loops.
There are literally no uses of while( have_posts() )
that I can find without a call to the_post()
directly after it. Not in core, not in most of the plugins I've seen use it, nor cases of custom WP_Query objects calling via while( $query->have_posts() )
.
#4
in reply to:
↑ 2
@
10 years ago
- Focuses template added
Replying to SergeyBiryukov
Sorry, I was editing at the same time! Adding it back
#5
@
10 years ago
Love this! Let's make it happen. I guarantee it'll cut down on amateur mistakes which is a good thing considering the loop & template tags are intended to be new-developer friendly.
#6
@
10 years ago
The Loop is probably the most confusing thing for new developers, so I'm all for anything that can make understanding it a little easier. I think this is a great patch!
#9
@
10 years ago
How many use cases are there really for using have_posts() without the_post()?
In a while loop — none, because that would result in an infinite loop. In an if statement though, that's perfectly valid, because most often you don't want to advance the posts cursor until you actually hit the loop.
I have mixed feelings about this, and although I do agree that "the loop" in its current form is a bit awkward, I don't see how have_the_post()
makes it any easier, especially once you start working with secondary loops, where you need wp_reset_postdata()
. It think it's already a bit too implicit with the_post()
and moving that inside have_the_post()
makes it even more implicit.
Sorry, I said 'shortcode' above, but it was autocorrected somehow from 'shortcut', or at least that's what I'm going to claim :)