I am pretty new in WordPress theme development and I have the following question about the page.php page contained into templates files (the file that describe the template for the static pages)
I am analazyng the page.php file contained into the twentythirten default theme, the code of this file is:
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that other
* 'pages' on your WordPress site will use a different template.
*
* @package WordPress
* @subpackage Twenty_Thirteen
* @since Twenty Thirteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php comments_template(); ?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
My doubts are:
1) page.php is the default template for the static pages but this file contain the posts loop:
<?php while ( have_posts() ) : the_post(); ?>
Why?
WordPress is designed so that a very basic theme only needs one template:
index.php
. If you create a theme with anindex.php
, that file will be used for rendering all your content: the archives, the main blog page, individual pages, and so on. And all it needs on it to basically work is the loop.To achieve this, virtually every piece of content in WordPress is technically a "post". Blog posts are posts. Individual pages are "posts". Even search results are "posts". And all you need to do to display them is loop through the objects that WordPress hands you and output them.
Obviously, it's more helpful if different types of posts are displayed in different ways, so WordPress provides ways of overriding this default behaviour. If you add a
page.php
template, then WordPress will invoke that for individual pages instead of the defaultindex.php
. See the Template Hierarchy diagram to see which template page will be used for a given type of post. (But note that they all fall through toindex.php
if no specific template exists.)Because of this, you'll normally see a "post" "loop" used for any given template type in WordPress, even if you're not actually going to loop more than once, and even if you're not actually outputting blog posts.
On a single Page, WordPress will only "loop" once at most, and you can probably assume that you'll always have one post to output, so if you really want to, you can replace the usual
while
loop with code to output a single post. However, because WordPress still hands you a "set" of posts that just happens to contain a single post, you still need to at least callthe_post()
to load up this single post into the variables that are used for the later output calls likethe_content()
. As the code is therefore virtually identical with a loop, and most people have already written the loop for their index.php template, normally people just leave it as a loop, knowing it'll only execute once.If you're developing a theme, you'll want to get very familiar with how the template hierarchy and loop work together to output WordPress content.
WordPress has many types of 'post'. The default 'post types' are page, attachment, nav_menu_item, post and revision.
You can even create your own post types using the
register_post_type()
function. Post types registered in this way are called 'custom post types'.Refs: