WordPress最核心、最不需要自定义的3类内容,也是本指南的唯一焦点:
用于发布新闻、博客、动态等“时效性内容”,支持分类、标签
用于创建“关于我们”“联系我们”等固定内容,支持子页面层级
用于给文章归类(如“新闻”“产品动态”),点击分类显示对应文章列表
本指南所有代码都只针对这3类内容,无需安装插件、无需自定义字段、无需改数据库,复制到主题文件夹即可用!
选择内容类型和参数,一键生成可直接使用的循环代码
分类别名在“文章-分类”中查看(如“新闻”的别名为“news”)
将代码复制到主题的 index.php(首页)、category.php(分类页)中,无需改任何参数!
3个核心模板文件,直接复制到主题文件夹即可用
<?php get_header(); ?>
<div class="content-area max-w-4xl mx-auto px-4 py-8">
<main class="site-main">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- 文章主体 -->
<article id="post-<?php the_ID(); ?>" <?php post_class( 'bg-white rounded-xl p-6 mb-8' ); ?>>
<!-- 文章标题 -->
<header class="entry-header mb-6">
<h1 class="entry-title text-2xl font-bold"><?php the_title(); ?></h1>
<!-- 文章日期、分类 -->
<div class="entry-meta text-sm text-gray-500 mt-4">
<span><i class="fa fa-calendar-o mr-2"></i><?php the_date( 'Y年m月d日' ); ?></span>
<span class="ml-4"><i class="fa fa-folder-o mr-2"></i><?php the_category( ',' ); ?></span>
</div>
</header>
<!-- 文章内容 -->
<div class="entry-content text-gray-700 leading-relaxed">
<?php the_content(); ?>
</div>
<!-- 上下篇导航 -->
<nav class="post-navigation mt-8 flex justify-between">
<div><?php previous_post_link( '上一篇:%link' ); ?></div>
<div><?php next_post_link( '下一篇:%link' ); ?></div>
</nav>
</article>
<!-- 评论区 -->
<?php if ( comments_open() ) : ?>
<div class="comments-area bg-white rounded-xl p-6">
<?php comments_template(); ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<div class="bg-white rounded-xl p-6 text-center">
<p class="text-gray-600">未找到相关文章</p>
</div>
<?php endif; ?>
</main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php get_header(); ?>
<div class="content-area max-w-4xl mx-auto px-4 py-8">
<main class="site-main">
<!-- 分类标题和描述 -->
<header class="page-header mb-8 text-center">
<h1 class="page-title text-2xl font-bold">分类:<?php single_cat_title(); ?></h1>
<?php if ( category_description() ) : ?>
<div class="category-description mt-4 text-gray-600">
<?php echo 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 overflow-hidden shadow-sm' ); ?>>
<!-- 文章缩略图 -->
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'medium', array( 'class' => 'w-full h-48 object-cover' ) ); ?>
</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">
<?php the_title(); ?>
</a>
</h2>
<div class="entry-meta text-xs text-gray-500 mb-3">
<?php the_date( 'Y年m月d日' ); ?>
</div>
<div class="entry-summary text-sm text-gray-600 line-clamp-3">
<?php the_excerpt(); ?>
</div>
<a href="<?php the_permalink(); ?>" class="inline-block mt-4 text-primary text-sm">
查看详情 <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>'
) );
?>
</div>
<?php else : ?>
<div class="bg-white rounded-xl p-8 text-center">
<i class="fa fa-folder-open text-gray-300 text-4xl mb-4"></i>
<p class="text-gray-600">该分类下暂无文章</p>
</div>
<?php endif; ?>
</main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php get_header(); ?>
<div class="content-area max-w-4xl mx-auto px-4 py-8">
<main class="site-main">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'bg-white rounded-xl p-6 mb-8' ); ?>>
<!-- 页面标题 -->
<header class="entry-header mb-6">
<h1 class="entry-title text-2xl font-bold"><?php the_title(); ?></h1>
</header>
<!-- 页面内容 -->
<div class="entry-content text-gray-700 leading-relaxed">
<?php the_content(); ?>
</div>
<!-- 子页面列表(如有) -->
<?php
$child_pages = get_pages( array(
'child_of' => get_the_ID(),
'sort_column' => 'menu_order'
) );
if ( $child_pages ) : ?>
<div class="child-pages mt-8 border-t border-gray-200 pt-6">
<h3 class="text-lg font-semibold mb-4">相关页面</h3>
<ul class="grid grid-cols-1 sm:grid-cols-2 gap-3">
<?php foreach ( $child_pages as $child ) : ?>
<li>
<a href="<?php echo get_permalink( $child->ID ); ?>" class="text-primary hover:text-primary/80">
<i class="fa fa-angle-right ml-1"></i> <?php echo $child->post_title; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</article>
<!-- 页面评论 -->
<?php if ( comments_open() ) : ?>
<div class="comments-area bg-white rounded-xl p-6">
<?php comments_template(); ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<div class="bg-white rounded-xl p-6 text-center">
<p class="text-gray-600">未找到相关页面</p>
</div>
<?php endif; ?>
</main>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>