结构几乎一样,但页面(page)没有这些东西:
所以 page.php 更简单:
<?php get_header(); ?>
<div class="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article>
<h1><?php the_title(); ?></h1>
<div class="post-content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; endif; ?>
</div>
<?php get_footer(); ?>
没有 meta、没有标签、没有评论(如果“关于”页不需要评论)。
看情况。“关于”页通常不需要评论,“联系方式”页可能需要。
如果想“按页面单独控制”,可以在后台每个页面的“讨论”面板里取消勾选“允许评论”,然后在模板里加判断:
<?php if ( comments_open() || get_comments_number() ) { comments_template(); } ?>
这样关掉评论的页面就不会输出评论区。
有时候某个页面要特殊布局(比如“全宽页面”“落地页”)。做法是建一个带模板声明的文件:
<?php
/* Template Name: 全宽页面 */
get_header();
?>
<div class="full-width">
<?php while ( have_posts() ) : the_post(); the_content(); endwhile; ?>
</div>
<?php get_footer(); ?>
关键是开头那行注释 Template Name: 全宽页面。有了它,后台编辑页面时,“页面属性”里会多出一个“模板”下拉框,可以选这个模板。
这就是模板层级里“自定义页面模板”那一级的来源(卷一第 2 篇讲过)。
后台“设置 → 阅读 → 首页显示:一个静态页面”,然后选两个页面(首页、文章页)。
这时候:
front-page.php(如果有);home.php(如果有);index.php。如果你想让首页长得跟普通页面不一样,就建一个 front-page.php。
一句话记住:page.php 比 single.php 简单(无分类/标签/时间);特殊布局用「Template Name」自定义模板。
下一篇做搜索页和 404。