V2 版本 · 2025年更新

WordPress原生内容核心模板

完整可运行的文章、页面和分类模板代码,无任何自定义功能,直接适配默认主题

模板简介

以下是WordPress最核心的三个模板文件,适用于默认的文章、页面和分类内容类型,无需任何自定义即可直接使用:

single.php

文章详情页模板,用于展示单篇文章的完整内容

page.php

页面模板,用于展示"关于我们"等固定页面内容

category.php

分类页模板,用于展示特定分类下的所有文章

使用指南

  1. 复制所需模板的完整代码
  2. 在WordPress主题文件夹中创建相应的PHP文件
  3. 将复制的代码粘贴到文件中并保存
  4. 在WordPress后台启用该主题即可生效

文章详情页模板

single.php - 用于展示单篇文章的完整内容

single.php

文章详情页 V2.0
<?php
/**
 * 文章详情页模板
 * 
 * 用于展示单篇文章的完整内容,包括标题、元信息、内容、分页和评论
 * 
 * @package WordPress
 * @subpackage 默认主题
 * @since 1.0
 * @updated 2.0
 */
get_header(); ?>

<div class="content-area max-w-4xl mx-auto px-4 py-8">
    <main class="site-main" role="main">
        <?php if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <!-- 文章主体 -->
                <article id="post-<?php the_ID(); ?>" <?php post_class( 'bg-white rounded-xl shadow-sm p-6 mb-8' ); ?>>
                    <!-- 文章标题 -->
                    <header class="entry-header mb-6">
                        <h1 class="entry-title text-2xl md:text-3xl font-bold text-gray-800">
                            <?php the_title(); ?>
                        </h1>
                        <!-- 文章元信息(日期、分类、作者) -->
                        <div class="entry-meta text-sm text-gray-500 flex flex-wrap gap-4 mt-4">
                            <span class="posted-on flex items-center">
                                <i class="fa fa-calendar-o mr-2"></i>
                                <time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>">
                                    <?php the_date( 'Y年m月d日' ); ?>
                                </time>
                            </span>
                            <span class="byline flex items-center">
                                <i class="fa fa-user-o mr-2"></i>
                                <span class="author vcard">
                                    <a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>">
                                        <?php echo esc_html( get_the_author() ); ?>
                                    </a>
                                </span>
                            </span>
                            <span class="cat-links flex items-center">
                                <i class="fa fa-folder-o mr-2"></i>
                                <?php the_category( ',' ); ?>
                            </span>
                            <?php if ( has_tag() ) : ?>
                                <span class="tag-links flex items-center">
                                    <i class="fa fa-tags mr-2"></i>
                                    <?php the_tags( '', ',', '' ); ?>
                                </span>
                            <?php endif; ?>
                        </div>
                    </header>

                    <!-- 文章缩略图(如果设置了特色图片) -->
                    <?php if ( has_post_thumbnail() ) : ?>
                        <div class="post-thumbnail mb-6">
                            <?php the_post_thumbnail( 'large', array( 
                                'class' => 'w-full h-auto rounded-lg shadow-sm',
                                'alt' => esc_attr( get_the_title() )
                            ) ); ?>
                        </div>
                    <?php endif; ?>

                    <!-- 文章内容 -->
                    <div class="entry-content text-gray-700 leading-relaxed">
                        <?php 
                        // 输出文章内容并确保安全
                        the_content( sprintf(
                            wp_kses(
                                __( '继续阅读 "%s"', 'textdomain' ),
                                array(
                                    'span' => array(
                                        'class' => array(),
                                    ),
                                )
                            ),
                            get_the_title()
                        ) );
                        
                        // 文章分页(若用 <!--nextpage--> 分割内容)
                        if ( wp_link_pages( array( 'echo' => 0 ) ) ) : ?>
                            <div class="page-links mt-8 border-t border-gray-200 pt-4">
                                <span class="page-links-title text-sm font-medium">分页:</span>
                                <?php wp_link_pages( array(
                                    'before' => '<span class="page-links-pages">',
                                    'after'  => '</span>',
                                    'link_before' => '<span class="page-numbers">',
                                    'link_after'  => '</span>',
                                ) ); ?>
                            </div>
                        <?php endif; ?>
                    </div>

                    <!-- 上下篇导航 -->
                    <nav class="post-navigation mt-8 border-t border-gray-200 pt-6 flex flex-col md:flex-row justify-between gap-4">
                        <div class="nav-previous">
                            <?php previous_post_link( 
                                '<div class="flex items-center text-primary hover:text-primary/80 transition-colors">
                                    <i class="fa fa-angle-left mr-2"></i> %link
                                </div>',
                                '上一篇:%title'
                            ); ?>
                        </div>
                        <div class="nav-next">
                            <?php next_post_link( 
                                '<div class="flex items-center text-primary hover:text-primary/80 transition-colors">
                                    %link <i class="fa fa-angle-right ml-2"></i>
                                </div>',
                                '下一篇:%title'
                            ); ?>
                        </div>
                    </nav>

                    <!-- 文章编辑链接(仅管理员可见) -->
                    <?php edit_post_link(
                        sprintf(
                            wp_kses(
                                __( '编辑 %s', 'textdomain' ),
                                array(
                                    'span' => array(
                                        'class' => array(),
                                    ),
                                )
                            ),
                            '<div class="edit-link mt-4 text-right">',
                            '</div>',
                            get_the_ID()
                        )
                    ); ?>
                </article>

                <!-- 评论区(WP默认功能) -->
                <?php if ( comments_open() || get_comments_number() ) : ?>
                    <div class="comments-area bg-white rounded-xl shadow-sm p-6">
                        <?php comments_template(); ?>
                    </div>
                <?php endif; ?>
            <?php endwhile; ?>
        <?php else : ?>
            <!-- 无文章时显示 -->
            <div class="bg-white rounded-xl shadow-sm p-6 text-center">
                <p class="text-gray-600">未找到相关文章,请尝试其他内容。</p>
            </div>
        <?php endif; ?>
    </main>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

功能说明

  • 显示文章完整标题和内容
  • 展示文章发布日期、作者、分类和标签
  • 支持特色图片展示
  • 支持文章分页(使用<!--nextpage-->标签)
  • 包含上下篇文章导航
  • 集成评论系统(可在后台设置)
  • 管理员可见的编辑链接
  • 增强的安全过滤和转义处理

页面模板

page.php - 用于展示"关于我们"等固定页面内容

page.php

固定页面 V2.0
<?php
/**
 * 页面模板
 * 
 * 用于展示"关于我们"等固定页面内容
 * 
 * @package WordPress
 * @subpackage 默认主题
 * @since 1.0
 * @updated 2.0
 */
get_header(); ?>

<div class="content-area max-w-4xl mx-auto px-4 py-8">
    <main class="site-main" role="main">
        <?php if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <!-- 页面主体 -->
                <article id="post-<?php the_ID(); ?>" <?php post_class( 'bg-white rounded-xl shadow-sm p-6 mb-8' ); ?>>
                    <!-- 页面标题 -->
                    <header class="entry-header mb-6">
                        <h1 class="entry-title text-2xl md:text-3xl font-bold text-gray-800">
                            <?php the_title(); ?>
                        </h1>
                    </header>

                    <!-- 页面缩略图(如果设置了特色图片) -->
                    <?php if ( has_post_thumbnail() ) : ?>
                        <div class="page-thumbnail mb-6">
                            <?php the_post_thumbnail( 'large', array( 
                                'class' => 'w-full h-auto rounded-lg shadow-sm',
                                'alt' => esc_attr( get_the_title() )
                            ) ); ?>
                        </div>
                    <?php endif; ?>

                    <!-- 页面内容 -->
                    <div class="entry-content text-gray-700 leading-relaxed">
                        <?php 
                        // 输出页面内容并确保安全
                        the_content( sprintf(
                            wp_kses(
                                __( '继续阅读 "%s"', 'textdomain' ),
                                array(
                                    'span' => array(
                                        'class' => array(),
                                    ),
                                )
                            ),
                            get_the_title()
                        ) );
                        
                        // 页面分页(若用 <!--nextpage--> 分割内容)
                        if ( wp_link_pages( array( 'echo' => 0 ) ) ) : ?>
                            <div class="page-links mt-8 border-t border-gray-200 pt-4">
                                <span class="page-links-title text-sm font-medium">分页:</span>
                                <?php wp_link_pages( array(
                                    'before' => '<span class="page-links-pages">',
                                    'after'  => '</span>',
                                    'link_before' => '<span class="page-numbers">',
                                    'link_after'  => '</span>',
                                ) ); ?>
                            </div>
                        <?php endif; ?>
                    </div>

                    <!-- 子页面列表(若当前页面有子页面,自动显示) -->
                    <?php 
                    $child_pages = get_pages( array(
                        'child_of' => get_the_ID(),
                        'sort_column' => 'menu_order',
                        'sort_order' => 'ASC'
                    ) ); 
                    if ( !empty( $child_pages ) ) : ?>
                        <div class="child-pages mt-8 border-t border-gray-200 pt-6">
                            <h3 class="text-lg font-semibold mb-4 text-gray-800">相关页面</h3>
                            <ul class="grid grid-cols-1 sm:grid-cols-2 gap-3">
                                <?php foreach ( $child_pages as $child ) : ?>
                                    <li>
                                        <a href="<?php echo esc_url( get_permalink( $child->ID ) ); ?>" class="flex items-center text-primary hover:text-primary/80 transition-colors">
                                            <i class="fa fa-angle-right mr-2"></i>
                                            <?php echo esc_html( $child->post_title ); ?>
                                        </a>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif; ?>

                    <!-- 页面编辑链接(仅管理员可见) -->
                    <?php edit_post_link(
                        sprintf(
                            wp_kses(
                                __( '编辑 %s', 'textdomain' ),
                                array(
                                    'span' => array(
                                        'class' => array(),
                                    ),
                                )
                            ),
                            '<div class="edit-link mt-4 text-right">',
                            '</div>',
                            get_the_ID()
                        )
                    ); ?>
                </article>

                <!-- 页面评论(若开启评论功能) -->
                <?php if ( comments_open() || get_comments_number() ) : ?>
                    <div class="comments-area bg-white rounded-xl shadow-sm p-6">
                        <?php comments_template(); ?>
                    </div>
                <?php endif; ?>
            <?php endwhile; ?>
        <?php else : ?>
            <!-- 无页面时显示 -->
            <div class="bg-white rounded-xl shadow-sm p-6 text-center">
                <p class="text-gray-600">未找到相关页面,请检查链接是否正确。</p>
            </div>
        <?php endif; ?>
    </main>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

功能说明

  • 显示页面标题和完整内容
  • 支持特色图片展示
  • 支持页面分页(使用<!--nextpage-->标签)
  • 自动显示子页面列表(如有)
  • 可选评论功能(默认关闭)
  • 管理员可见的编辑链接
  • 适用于"关于我们"、"联系我们"等固定页面
  • 增强的安全过滤和转义处理

分类页模板

category.php - 用于展示特定分类下的所有文章

category.php

分类文章列表 V2.0
<?php
/**
 * 分类页模板
 * 
 * 用于展示特定分类下的所有文章列表
 * 
 * @package WordPress
 * @subpackage 默认主题
 * @since 1.0
 * @updated 2.0
 */
get_header(); ?>

<div class="content-area max-w-4xl mx-auto px-4 py-8">
    <main class="site-main" role="main">
        <!-- 分类页标题与描述 -->
        <header class="page-header mb-8 text-center">
            <h1 class="page-title text-2xl md:text-3xl font-bold text-gray-800">
                分类:<?php single_cat_title(); ?>
            </h1>
            <?php if ( category_description() ) : ?>
                <div class="category-description mt-4 text-gray-600 max-w-2xl mx-auto">
                    <?php echo wp_kses_post( category_description() ); ?>
                </div>
            <?php endif; ?>
        </header>

        <!-- 分类下的文章列表 -->
        <?php if ( have_posts() ) : ?>
            <div class="posts-list grid grid-cols-1 md:grid-cols-2 gap-6">
                <?php while ( have_posts() ) : the_post(); ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class( 'bg-white rounded-xl shadow-sm overflow-hidden' ); ?>>
                        <!-- 文章缩略图(若有) -->
                        <?php if ( has_post_thumbnail() ) : ?>
                            <a href="<?php the_permalink(); ?>" class="block">
                                <?php the_post_thumbnail( 'medium', array( 
                                    'class' => 'w-full h-48 object-cover',
                                    'alt' => esc_attr( get_the_title() )
                                ) ); ?>
                            </a>
                        <?php endif; ?>

                        <!-- 文章内容 -->
                        <div class="p-5">
                            <h2 class="entry-title text-lg font-semibold mb-2">
                                <a href="<?php the_permalink(); ?>" class="text-gray-800 hover:text-primary transition-colors">
                                    <?php the_title(); ?>
                                </a>
                            </h2>
                            <div class="entry-meta text-xs text-gray-500 mb-3">
                                <span class="post-date">
                                    <time datetime="<?php echo esc_attr( get_the_date( 'c' ) ); ?>">
                                        <?php the_date( 'Y年m月d日' ); ?>
                                    </time>
                                </span>
                                <?php if ( get_comments_number() ) : ?>
                                    <span class="comments-link ml-3">
                                        <a href="<?php comments_link(); ?>">
                                            <i class="fa fa-comment-o mr-1"></i><?php comments_number( '0', '1', '%' ); ?>
                                        </a>
                                    </span>
                                <?php endif; ?>
                            </div>
                            <div class="entry-summary text-gray-600 text-sm line-clamp-3">
                                <?php the_excerpt(); ?>
                            </div>
                            <a href="<?php the_permalink(); ?>" class="inline-block mt-4 text-primary hover:text-primary/80 text-sm font-medium">
                                查看详情 <i class="fa fa-angle-right ml-1"></i>
                            </a>
                        </div>
                    </article>
                <?php endwhile; ?>
            </div>

            <!-- 分类页分页 -->
            <div class="pagination mt-10 flex justify-center">
                <?php
                the_posts_pagination( array(
                    'prev_text' => '<i class="fa fa-angle-left"></i> 上一页',
                    'next_text' => '下一页 <i class="fa fa-angle-right"></i>',
                    'mid_size' => 2,
                    'screen_reader_text' => '分类页分页'
                ) );
                ?>
            </div>
        <?php else : ?>
            <!-- 分类下无文章时显示 -->
            <div class="bg-white rounded-xl shadow-sm p-8 text-center">
                <i class="fa fa-folder-open text-gray-300 text-4xl mb-4"></i>
                <p class="text-gray-600">该分类下暂无文章,敬请期待后续更新。</p>
                <a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="inline-block mt-6 text-primary hover:text-primary/80 transition-colors">
                    返回首页 <i class="fa fa-home ml-1"></i>
                </a>
            </div>
        <?php endif; ?>
    </main>
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

功能说明

  • 显示分类名称和描述(如有)
  • 以网格布局展示分类下的所有文章
  • 显示文章缩略图、标题、日期和摘要
  • 显示评论数量
  • 包含分页导航,支持多页浏览
  • 当分类下无文章时显示友好提示
  • 增强的安全过滤和转义处理
  • 响应式布局,适配各种设备

V2版本更新日志

V2.0 2025年9月更新
  • 增强安全性:为所有输出内容添加了适当的安全过滤函数(如esc_html、esc_url、wp_kses等)
  • 完善文档注释:为每个模板添加了标准的WordPress文档头部注释,包含版本信息和功能说明
  • 支持特色图片:在文章和页面模板中添加了特色图片展示功能
  • 增强元信息:在文章模板中添加了作者信息和标签展示
  • 编辑链接:添加了仅管理员可见的编辑链接,方便内容管理
  • 响应式优化:改进了布局在移动设备上的显示效果
  • 评论计数:在分类列表中添加了评论数量显示
  • 代码规范:统一了代码缩进和格式,添加了适当的注释说明
V1.0 初始版本
  • 基础的文章、页面和分类模板功能
  • 基本的响应式布局
  • 简单的代码复制功能