下面我将从基础结构核心模板文件常用函数完整示例以及进阶技巧五个方面,为你详细解析 WordPress 文章模板。

wordpress 文章模板
(图片来源网络,侵删)

基础结构:WordPress 的模板层级

WordPress 不会直接使用 single.php 来显示所有文章,它会遵循一套“模板层级”(Template Hierarchy)规则来寻找最合适的模板文件,这对于制作复杂主题非常有用。

对于单篇文章,WordPress 的查找顺序如下:

  1. single-{post-type}.php (single-post.php, single-product.php)
  2. single.php
  3. singular.php
  4. index.php

这意味着:

  • 如果你只想为默认的“文章”(Post)创建一个特殊模板,你可以创建 single-post.php
  • 如果你为所有文章类型(包括自定义文章类型)创建一个通用模板,你可以创建 single.php
  • singular.php 是所有单个页面(文章、页面、自定义文章类型)的父模板。
  • 如果以上文件都不存在,WordPress 最终会回退到 index.php

我们通常讨论的“文章模板”主要指 single.phpsingle-post.php

wordpress 文章模板
(图片来源网络,侵删)

核心模板文件:single.php 的结构

一个标准的 single.php 文件通常包含以下部分:

<?php
/**
 * Template Name: 文章模板 - 简约风格
 * Template Post Type: post // 可选,限制此模板只对文章类型生效
 */
?>
<?php get_header(); ?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">
            <?php
            while ( have_posts() ) : the_post();
                // 1. 文章头部 (标题、元信息)
                get_template_part( 'template-parts/content', get_post_format() );
                // 2. 文章导航 (上一篇文章/下一篇文章)
                the_post_navigation();
                // 3. 文章评论区
                // If comments are open or we have at least one comment, load up the comment template.
                if ( comments_open() || get_comments_number() ) :
                    comments_template();
                endif;
            endwhile; // End of the loop.
            ?>
        </main><!-- #main -->
    </div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

常用函数详解

single.php 的核心是 The Loop(WordPress 主循环),在循环内部,你可以使用各种函数来调用文章的内容。

类别 函数 描述 示例
the_title() 显示文章标题。 <h1><?php the_title(); ?></h1>
the_permalink() 显示文章的永久链接(URL)。 <a href="<?php the_permalink(); ?>">
the_content() 显示文章的完整内容。 <?php the_content(); ?>
the_excerpt() 显示文章的摘要(自动截取或手动设置的摘要)。 <?php the_excerpt(); ?>
文章元信息 the_time() 显示文章的发布时间。 发布于: <?php the_time('Y-m-d'); ?>
the_author() 显示文章的作者。 作者: <?php the_author(); ?>
the_category() 显示文章所属的分类。 分类: <?php the_category(', '); ?>
the_tags() 显示文章的标签。 标签: <?php the_tags(); ?>
文章特色图 has_post_thumbnail() 检查文章是否有设置特色图。 <?php if ( has_post_thumbnail() ) : ?>
the_post_thumbnail() 显示文章的特色图。 <?php the_post_thumbnail( 'large' ); ?>
文章导航 previous_post_link() 显示上一篇文章的链接。 <?php previous_post_link(); ?>
next_post_link() 显示下一篇文章的链接。 <?php next_post_link(); ?>
the_post_navigation() 显示更美观的上一篇文章/下一篇文章导航。 <?php the_post_navigation(); ?>
评论 comments_template() 加载 comments.php 文件,显示评论区。 <?php comments_template(); ?>

完整的 single.php 示例

这是一个功能完整、结构清晰的 single.php 模板示例,你可以基于它进行修改。

<?php
/**
 * 文章模板文件
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package WordPress
 * @subpackage Twenty_Twenty_One
 * @since Twenty Twenty-One 1.0
 */
get_header(); ?>
<?php if ( have_posts() ) : ?>
    <?php
    // 开始 WordPress 主循环
    while ( have_posts() ) :
        the_post();
        ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <!-- 文章头部 -->
            <header class="entry-header">
                <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
                <div class="entry-meta">
                    <?php
                    twenty_twenty_one_posted_by();
                    twenty_twenty_one_posted_on();
                    ?>
                </div><!-- .entry-meta -->
            </header><!-- .entry-header -->
            <!-- 如果有特色图,则显示 -->
            <?php if ( has_post_thumbnail() ) : ?>
                <div class="post-thumbnail">
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail( 'post-thumbnail' ); ?>
                    </a>
                </div>
            <?php endif; ?>
            <!-- 文章内容 -->
            <div class="entry-content">
                <?php
                the_content(
                    sprintf(
                        wp_kses(
                            /* translators: %s: Post title. Only visible to screen readers. */
                            __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentytwentyone' ),
                            array(
                                'span' => array(
                                    'class' => array(),
                                ),
                            )
                        ),
                        get_the_title()
                    )
                );
                wp_link_pages(
                    array(
                        'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwentyone' ),
                        'after'  => '</div>',
                    )
                );
                ?>
            </div><!-- .entry-content -->
            <!-- 文章底部元信息(标签等) -->
            <footer class="entry-footer">
                <?php twenty_twenty_one_entry_footer(); ?>
            </footer><!-- .entry-footer -->
        </article><!-- #post-<?php the_ID(); ?> -->
        <!-- 文章导航 -->
        <?php the_post_navigation(
            array(
                'prev_text' => '<span class="nav-subtitle">' . __( 'Previous:', 'twentytwentyone' ) . '</span> <span class="nav-title">%title</span>',
                'next_text' => '<span class="nav-subtitle">' . __( 'Next:', 'twentytwentyone' ) . '</span> <span class="nav-title">%title</span>',
            )
        ); ?>
        <!-- 评论区 -->
        <?php
        // 如果评论开启或已有评论,则加载评论模板
        if ( comments_open() || get_comments_number() ) :
            comments_template();
        endif;
        ?>
    <?php
    // 结束主循环
    endwhile;
else :
    // 如果没有找到文章,则加载内容-none.php
    get_template_part( 'template-parts/content', 'none' );
endif;
?>
<?php
get_sidebar();
get_footer();

进阶技巧与最佳实践

a. 使用 get_template_part() 模块化你的代码

将文章的不同部分(如内容、作者信息等)拆分成独立的文件,是一个非常好的习惯,这可以让你的代码更整洁、易于维护和复用。

wordpress 文章模板
(图片来源网络,侵删)
  1. 在主题根目录下创建 template-parts 文件夹。

  2. template-parts 文件夹中创建 content.php 文件(用于存放文章主体内容)。

  3. 修改你的 single.php 文件:

    // 在 single.php 的 The Loop 中
    while ( have_posts() ) : the_post();
        get_template_part( 'template-parts/content', get_post_format() ); // get_post_format() 可以根据文章格式(如aside, image等)加载不同的内容模板
    endwhile;
  4. template-parts/content.php 中编写你的文章内容结构:

    <?php
    /**
     * 模板部分:文章内容
     *
     * @package WordPress
     * @subpackage Your_Theme
     */
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <header class="entry-header">
            <?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
        </header>
        <?php if ( has_post_thumbnail() ) : ?>
            <div class="post-thumbnail">
                <?php the_post_thumbnail(); ?>
            </div>
        <?php endif; ?>
        <div class="entry-content">
            <?php the_content(); ?>
        </div>
        <footer class="entry-footer">
            <?php // 在这里放置作者、分类、标签等信息 ?>
        </footer>
    </article>

b. 条件判断函数

你可以使用条件判断函数来为不同类型的文章显示不同的内容。

  • is_sticky(): 判断文章是否为“置顶”文章。
  • in_category( 'category-slug' ): 判断文章是否属于某个特定分类。
  • has_tag( 'tag-slug' ): 判断文章是否包含某个特定标签。

示例:

<?php if ( is_sticky() ) : ?>
    <p class="sticky-post">置顶文章</p>
<?php endif; ?>
<?php if ( in_category( 'news' ) ) : ?>
    <div class="news-badge">新闻</div>
<?php endif; ?>

c. 使用自定义字段

如果文章有额外的信息(如作者简介、引用来源等),可以使用自定义字段

  1. 在文章编辑页面的“自定义字段”模块中添加字段(如 source)和值。

  2. single.php 中使用 get_post_meta() 函数获取它:

    $source_url = get_post_meta( get_the_ID(), 'source_url', true );
    $source_name = get_post_meta( get_the_ID(), 'source_name', true );
    if ( $source_url ) {
        echo '<p>来源: <a href="' . esc_url( $source_url ) . '">' . esc_html( $source_name ) . '</a></p>';
    }

创建一个 WordPress 文章模板,你需要:

  1. 理解模板层级:知道 single.php 在整个加载流程中的位置。
  2. 掌握 The Loop:这是 WordPress 显示动态内容的基石。
  3. 熟悉核心函数:知道如何用 the_* 系列函数调用文章的各个部分。
  4. 实践模块化:使用 get_template_part() 将代码拆分,提高可维护性。
  5. 善用条件判断:让你的模板更具灵活性。

希望这份详细的指南能帮助你更好地理解和创建 WordPress 文章模板!