3

Trying to wrap my head around the loop concept...

Given a template page (page-work.php) with just this code:

<?php the_title(); ?>

WP correctly shows the title of that page. As I understand it, this is outside any loop and shouldn't work (Codex says to use get_the_title in those cases).

So the question is, why does it work?

Funnily enough, this won't output the title + content, just the title:

<?php the_title(); the_content(); ?>

Thanks!

2

The global $post object exists already for singular views before wp_head is called. It is just not filled with all data.

The the_title() calls get_the_title() which in turn calls get_post(). And that calls $GLOBALS['post'] if no post ID has been passed.

See also Generating the ogp tags in theme for a use case.

  • I see! So this has to do with the global $post, not the loop. So much new stuff for me :) Thanks a lot! By the way, do you know of any online resource where I can learn more about the WP globals and the rendering/processing order of WP? The order in which WP assembles everything. I get completely lost at the Codex sometimes and English is not my primary language so searching for that is tricky. – MrMerrick Oct 3 '13 at 11:55
  • @MrMerrick I am not aware of any really good documentation for this. Global variables are hard to understand by their very nature. You might try my plugin T5 WP Load Order. I made that to understand when which file, variable etc. is available. – fuxia Oct 3 '13 at 12:07
  • Thanks a lot Toscho, will take a look at your plugin and get a better grasp of what's happening behind the scenes. Much appreciated! – MrMerrick Oct 3 '13 at 12:11

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.