【发布时间】:2014-07-14 08:48:22
【问题描述】:
我是 WordPress 主题开发的新手,我有以下关于模板文件中包含的 page.php 页面(描述静态页面模板的文件)的问题
我正在分析 twentythirten 默认主题中包含的 page.php 文件,该文件的代码是:
<?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(); ?>
我的疑惑是:
1) page.php 是静态页面的默认模板,但此文件包含帖子循环:
<?php while ( have_posts() ) : the_post(); ?>
为什么?
【问题讨论】:
-
Pages 也称为 Post,但其 Post 类型是 Pages
-
虽然 page.php 并不真正保证“循环”,因为它通常只处理单个页面,但在 WP 开发中使用循环似乎是传统的。在“page.php”上,这个循环通常最多执行一次。我猜想使用循环是传统的做法,因为您通常首先编写一个模板—index.php,它有一个循环,然后将其复制为任何更具体模板的起点(如果您不提供page.php,则默认会使用 index.php,因此将 index.php 复制粘贴到 page.php 是开始开发自定义 page.php 的正常方式。)
标签: php wordpress-theming wordpress