I'm working with a custom theme that someone else created, and it's becoming obvious the author(s) were not familiar with WordPress. I'm doing cleanup and I'm just finding all sorts of bad things. They use ACF heavily and as such, have decided that using "the loop" is not necessary. Page templates essentially only have the following:
- comment block with template name
- get_header()
- a series of ACF get_field() calls
- get_footer()
This works, but I've never seen page templates created like this.
My question is not just what best practice is (I feel like this should all be wrapped in the standard have_posts/the_post loop even if it's only a single post/page template), but how does this work without even a call to "the_post()" AND is there likely some danger that some future version of WP will break this method of getting post data.
I'm working with a custom theme that someone else created, and it's becoming obvious the author(s) were not familiar with WordPress. I'm doing cleanup and I'm just finding all sorts of bad things. They use ACF heavily and as such, have decided that using "the loop" is not necessary. Page templates essentially only have the following:
- comment block with template name
- get_header()
- a series of ACF get_field() calls
- get_footer()
This works, but I've never seen page templates created like this.
My question is not just what best practice is (I feel like this should all be wrapped in the standard have_posts/the_post loop even if it's only a single post/page template), but how does this work without even a call to "the_post()" AND is there likely some danger that some future version of WP will break this method of getting post data.
Share Improve this question asked Jun 16, 2019 at 19:28 sporkersporker 435 bronze badges1 Answer
Reset to default 2but how does this work without even a call to "the_post()"
Template tags like the_title()
, the_content()
etc., as well as the ACF get_field()
functions, all use the global $post
variable to determine which post's title, content etc. to display (unless a specific ID is specified).
When a WordPress page is loaded, WordPress performs the "main" query, which is what determines which posts will be used in The Loop. It also parses the URL and determines what to query, sends headers, and handles 404s.
The last thing it does though, is assign some global variables. One of those is the global $post
variable. This is automatically set to the first post in the query's results. It also sets a couple of other global variables.
Because the global $post
variable is set at this early stage, functions like the ones I mentioned earlier will generally work as expected when on a single page or post, even without The Loop.
The reason this is not sufficient though, is that the the_post()
function that is run during the loop does several things that the main query has not already done. This includes:
- Firing the
loop_start
action. - Iterating to the next post in the loop, when there are more than one.
- Setting the
in_the_loop()
totrue
. - Setting several global variables not set in the main query, including:
$id
,$authordata
,$currentday
,$currentmonth
,$page
,$pages
,$multipage
, and$numpages
.
So if you don't run while ( have_posts() ) : the_post()
then none of the above will happen. In many circumstances, including — apparently — yours, this might not seem to cause any trouble. But any plugins or other functionality that relies on any of the above will probably not work correctly.
is there likely some danger that some future version of WP will break this method of getting post data.
It's arguably already broken. I would be surprised if there was a change in the short to medium term that would break anything that's currently working for you (not setting the global $post variable during the main query would likely break many sites), but the only way to be safe is to do things the correct way.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745394468a4625814.html
评论列表(0条)