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;
- 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"
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;
- 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!