最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Where is the WP_Query instance instantiated (when working with themes)?

programmeradmin5浏览0评论

This may be a very basic question, but I could not find the answer. The following documentation shows the code

// The Query
$the_query = new WP_Query( $args );

but when I look at wp-contents/themese/twentytwelve/index.php I see calls to have_posts() and the_post() inside the loop with no reference to $the_query as though index.php was included from inside a WP_Query instance. Can someone please explain what is happening here?

Thanks.

This may be a very basic question, but I could not find the answer. The following documentation http://codex.wordpress.org/Class_Reference/WP_Query shows the code

// The Query
$the_query = new WP_Query( $args );

but when I look at wp-contents/themese/twentytwelve/index.php I see calls to have_posts() and the_post() inside the loop with no reference to $the_query as though index.php was included from inside a WP_Query instance. Can someone please explain what is happening here?

Thanks.

Share Improve this question asked Sep 10, 2014 at 18:28 John SondersonJohn Sonderson 1991 gold badge2 silver badges10 bronze badges 1
  • If the answer was helpful to you, then consider accepting it. See »What should I do when someone answers my question?« and/or »Why is voting important?«, more information about the WordPress Development model is available at the help center. – Nicolai Grossherr Commented Apr 28, 2015 at 14:33
Add a comment  | 

2 Answers 2

Reset to default 1

This is because WP_Query is user generated when you want to display your recent blog posts, featured posts, posts with a tag, the best place to find them is in shortcodes folder. All other files serve as templates to iterate, filter and show results.

You have to read the documentation thoroughly, for example:

The second is during The Loop. WP_Query provides numerous functions for common tasks within The Loop. To begin with, have_posts(), which calls $wp_query->have_posts(), is called to see if there are any posts to show. If there are, a while loop is begun, using have_posts() as the condition. This will iterate around as long as there are posts to show. In each iteration, the_post(), which calls $wp_query->the_post() is called, setting up internal variables within $wp_query and the global $post variable (which the Template Tags rely on), as above. These are the functions you should use when writing a theme file that needs a loop. See also The Loop and The Loop in Action for more information.

Section: WP_Query - Interacting with WP_Query

And to confirm it look up the source code of have_posts():

739 /**
740  * Whether current WordPress query has results to loop over.
741  *
742  * @see WP_Query::have_posts()
743  * @since 1.5.0
744  * @uses $wp_query
745  *
746  * @return bool
747  */
748 function have_posts() {
749         global $wp_query;
750 
751         return $wp_query->have_posts();
752 }

Now you can be sure, the main query does work with the global variable $wp_query.

But you really just have to actually read the documentation and start reading some code, than this wouldn't actually be a question.

发布评论

评论列表(0)

  1. 暂无评论