Reputation: 42957
I am very new in WordPress development (and also in PHP development, I came from Java and C#). Today I have installed Aptana and configured XDebug to inspect what happens into the TwentyTwelve theme.
In particular I am trying to understand how WordPress load the posts by the loop (debbuging it).
So, since now, I only know how create a loop in a WP theme but I don't know how exactly it work.
So, in the TwentyTwelve I find the post loop, this code snippet:
From what I can understand the loop is composed by a while cycle where the have_posts() result is the condition to execute the while body.
Ok, now probabily the following question is related to my ignorance in PHP but what exactly means the construct:
<?php while ( have_posts() ) : the_post(); ?>
have_posts() is the condition to execute in the while but what exatly means the : the_post();
Is the_post() call the first operation of the cycle or what?
Then I have put a breackpoint into the have_posts() function definied into the query.php file.
function have_posts() {
global $wp_query;
return $wp_query->have_posts();
}
What exatly is the query.php file? Is it a class or what? Looking its code seems to me that it is not a class...why?
And what exactly represent $wp_query variable? Is it an object or what? (PHP is not typed so how can I know what a variable contains?)
What means this operation:
$wp_query->have_posts();
I think that it call another version of the have_posts() function (definied into the query.php file) that check if exist some other post to show or if they are ended.
But how exactly work? Reading the PHP OO documentation the -> operator call a method of an object but I don't know if $wp_query is an object or if in this contes the -> operator have another sense...
Can you help me to understand how exactly it work?
Tnx
Upvotes: 0
Views: 210
Reputation: 2142
To understand how loop works in wordpress you will have to understand some basics in php
before the function <?php while ( have_posts() ) : the_post(); ?>
there is a global query of arrays that is in the wordpress post array format [0][1][2][3]
so while have post the post just means, if there is a post, that element is set to THE POST
it sets all calls to relay to contents in that single array http://codex.wordpress.org/Function_Reference/the_post
for example when you call the_content()
you are displaying that post's index and inside there there is a 'content' => 'contents goes here'
long story short, learn the wordpress predefined syntax and how loop works on their documentation site.
located here http://codex.wordpress.org/Function_Reference/
Upvotes: 0