2

I have some questions about the loop. I'm using "twentyfourteen" theme as an example. I'm creating 2 php files with basic loop. One is home.php, and one is a template page called sample-page.php . Both contains this code;

if( have_posts() ) :
    while( have_posts() ) : the_post();
        the_content;
    endwhile;
endif;

Nothing fancy, the only difference is I have template declaration on sample-page.php

/**
 * Template Name: Sample Page
 */

According to WordPress Codex, when you set a "Blog" as a static front-page, a home.php is used as template, if it exists. The "Static Front-page" will use the custom page template, if it exists, in my example, it would use sample-page.php . Now here's my questions;

  1. Why is it that 2, identical php file produce different results? In my example, the "sample-page.php" will display the "PAGE CONTENT", while "home.php" will display "RECENT POSTS"
  2. Why do we need, "while" loop, if all we want is to display the "PAGE CONTENT"? Can't we just use

    if( have_posts() ) :
        the_post();
        the_content();
    endif;
    
  3. If we want to show "RECENT POSTS" in a template page. Why do we need to provide some queries first?

These questions might seems unnecessary and don't really have a purpose. Even my friend told me, "As long as you know how to drive a car, you don't need to understand how the engine works." But still I want to know. I would really appreciate if someone can give me some answers about this. Even if you don't know the answer, I thank you for reading!

Regards,

Dan

3 Answers 3

1

This quite a broad question to answer. I'm not going to go into detail here and into core code, you'll need to go and read the links I'm going to provide. I'm going to try to keep my answer sweet and short and useful :-)

Take the bundled theme twentyfourteen for example, when you open any page template, you'll find this piece of code

if( have_posts() ) :
    while( have_posts() ) : the_post();
    <----loop elements---->
    endwhile;
endif;

This is your loop, which basically displays the queried information from the main loop on the spesific template. That is all the loop does, it displayes what is retrieved by the main query (or custom query if there is one).

What to display where is decided by the Template Hierarchy. Wordpress relies heavily on this hierarchy structure. If you have a look at how the main query works, ( go and read Query Overview) you'll see that the main query uses this template hierarchy to decide which template to use and what information to retrieve from the database, this is why the information will be diffirent for, lets say, category.php and author.php, although your loop is exactly the same as you say

As per your question

Why is it that 2, identical php file produce different results? In my example, the "sample-page.php" will display the "PAGE CONTENT", while "home.php" will display "RECENT POSTS"

The abovementioned info basically covers this part, aswell, the main query uses post_type=page when it makes a query on page templates, and post_type=post on home.php, that is one of the big differences

Why do we need, "while" loop, if all we want is to display the "PAGE CONTENT"

The while() loop is not necessary on page templates. You'll only have one post to display.

If we want to show "RECENT POSTS" in a template page. Why do we need to provide some queries first?

By default, the main query queries post_type=page for page templates, not post-type=post. That is why by default you cannot display "recent posts" on pages. For this you'll need to run a custom query with WP_Query or alter the main query using pre_get_posts

EDIT

You can also go and have a look at my answer for further info

4
  • Hey Pieter, do you know which file that decides post_type=page for page templates, and post_type=post for home.php? Jul 6, 2014 at 3:57
  • Sorry for my late reply, had a power failure, so I could not use my computer :-(. Go and have a look at the WP_Post class in wp-includes/post.php from line 452. You'll see there that the default pot_type is post which is used for all templates. When you scroll down to line 662, you'll see that if you are on a page template, the post_type is changed to page Jul 6, 2014 at 13:22
  • Thank you Pieter. Your answer really helps me alot. Thanks again. Jul 9, 2014 at 5:03
  • My pleasure. Glad I could help Jul 9, 2014 at 5:06
1

For showing data in static template field you need to run WP_Query with your arguments

Then code will look like this

<?php
$args = array('post_type'=>'post');
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
0

Why is it that 2, identical php file produce different results? In my example, the "sample-page.php" will display the "PAGE CONTENT", while "home.php" will display "RECENT POSTS"

Because there are two different things that can be at front page, depending on how you configure it — list of blog posts or front page (with list of posts displayed elsewhere, in line with rest of configuration in settings).

Why do we need, "while" loop, if all we want is to display the "PAGE CONTENT"? Can't we just use

Yep, that would work just fine. while loop is used when same template or template part might be used in both kinds of templates. Or when someone just copy/pasted it around.

If we want to show "RECENT POSTS" in a template page. Why do we need to provide some queries first?

Not clear what you are asking. Typically anything in WordPress displays posts from main query in main loop. To change these posts main query needs to be adjusted (via pre_get_posts hook for example). To show more of other posts additional query must be run.

Your Answer

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

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