以下是WordPress最核心的三个模板文件,适用于默认的文章、页面和分类内容类型,无需任何自定义即可直接使用:
文章详情页模板,用于展示单篇文章的完整内容
页面模板,用于展示"关于我们"等固定页面内容
分类页模板,用于展示特定分类下的所有文章
single.php - 用于展示单篇文章的完整内容
<?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(); ?>
page.php - 用于展示"关于我们"等固定页面内容
<?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(); ?>
category.php - 用于展示特定分类下的所有文章
<?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(); ?>