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 in the loop (debbuging it).

So, up until 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:

<?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>

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 does this construction means:

<?php while ( have_posts() ) : the_post(); ?>

have_posts() is the condition to execute in the while but what exactly means the : the_post();

Is the_post() call the first operation of the cycle or what?

Then I have put a breakpoint into the have_posts() function definied into the query.php file.

function have_posts() {
    global $wp_query;

    return $wp_query->have_posts();

What exactly is the query.php file? Is it a class or what? Looking at its code it 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:


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 contents the -> operator have another sense...

Can you help me to understand how exactly it work?

1 Answer 1

if (have_posts()) : while (have_posts()) : the_post();                                     endwhile;
 _e('No blog found.', 'polymer');

if(have_post) condition is to check if the page contain post, while(have_posts) condition if there is more than one post display in loop, the_post() is used to display the post if(have_post) condition fails it will go to else loop. Tell for any further doubts.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.