---
canonical: https://meta.cyberforums.com/t/theme/2417
webpage: https://wpcy.com/community/t/2417-theme
language: zh-Hans
---

# 主题开发专家 @theme

> 文派叶子支持论坛的只读副本，本页不能回复。
> 讨论原文：https://meta.cyberforums.com/t/theme/2417
> 网页版：https://wpcy.com/community/t/2417-theme

## 1 · 诗语

- 发布时间：2026年1月29日 16:44

@theme 你好！请问如何创建一个简单的 WordPress 子主题？我想基于 Twenty Twenty-Four 主题创建一个子主题，需要修改一些样式和模板。请提供详细的步骤和代码示例。

## 2 · system

- 发布时间：2026年1月29日 16:45

你好 @feibisi 诗语！
很高兴为您解答关于 WordPress 子主题开发的问题。基于 Twenty Twenty-Four 这个区块主题创建子主题，步骤与经典主题略有不同，但同样简单。下面我将为您提供详细的步骤和代码示例。
创建 Twenty Twenty-Four 子主题的完整步骤
1. 创建子主题目录
在 WordPress 的 /wp-content/themes/ 目录下，创建一个新文件夹，例如：
twentytwentyfour-child

2. 创建必需的样式表文件 (style.css)
在子主题目录中创建 style.css 文件，内容如下：
/*
Theme Name:   Twenty Twenty-Four Child
Theme URI:    https://wpcommunity.com/
Description:  Twenty Twenty-Four 子主题
Author:       您的名字
Author URI:   https://wpcommunity.com/
Template:     twentytwentyfour
Version:      1.0.0
License:      GNU General Public License v2 or later
License URI:  http://www.gnu.org/licenses/gpl-2.0.html
Text Domain:  twentytwentyfour-child
*/

/* 在这里添加您的自定义 CSS */

关键点说明：

Template: twentytwentyfour 必须与父主题目录名完全一致
文本域 Text Domain 用于国际化，应与子主题目录名一致

3. 创建 functions.php 文件
在子主题目录中创建 functions.php 文件，内容如下：
<?php
/**
 * Twenty Twenty-Four 子主题
 * 
 * @package twentytwentyfour-child
 */

// 安全检查
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * 加载父主题样式表
 */
function twentytwentyfour_child_enqueue_styles() {
    // 加载父主题样式
    wp_enqueue_style(
        'parent-style',
        get_template_directory_uri() . '/style.css'
    );
    
    // 加载子主题样式
    wp_enqueue_style(
        'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( 'parent-style' ),
        wp_get_theme()->get( 'Version' )
    );
}
add_action( 'wp_enqueue_scripts', 'twentytwentyfour_child_enqueue_styles' );

/**
 * 可选：添加自定义功能
 */
function twentytwentyfour_child_setup() {
    // 加载子主题文本域
    load_child_theme_textdomain(
        'twentytwentyfour-child',
        get_stylesheet_directory() . '/languages'
    );
    
    // 添加主题支持（如果需要）
    add_theme_support( 'custom-logo' );
}
add_action( 'after_setup_theme', 'twentytwentyfour_child_setup' );

4. 创建 screenshot.png（可选但推荐）
创建一个 1200×900 像素的截图，展示您的子主题外观。
5. 激活子主题

登录 WordPress 后台
进入「外观」→「主题」
找到您的子主题并点击「启用」

针对区块主题的特殊处理
由于 Twenty Twenty-Four 是区块主题，您可能还需要：
1. 创建 theme.json 文件（用于覆盖全局样式）
在子主题目录中创建 theme.json 文件：
{
  "version": 2,
  "settings": {
    "color": {
      "palette": [
        {
          "slug": "primary",
          "color": "#1a4548",
          "name": "主色"
        }
      ]
    },
    "typography": {
      "fontSizes": [
        {
          "slug": "medium",
          "size": "20px",
          "name": "中等"
        }
      ]
    }
  },
  "styles": {
    "color": {
      "background": "#ffffff",
      "text": "#333333"
    },
    "typography": {
      "fontSize": "18px"
    }
  }
}

2. 覆盖模板文件（如果需要）
要修改模板，只需在子主题中创建同名文件：
twentytwentyfour-child/
├── templates/
│   └── index.html    # 覆盖首页模板
└── parts/
    └── header.html   # 覆盖页头部分

3. 添加自定义样式
在 style.css 中添加您的自定义样式：
/* 自定义按钮样式 */
.wp-block-button__link {
    border-radius: 8px;
    transition: all 0.3s ease;
}

.wp-block-button__link:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* 自定义标题样式 */
h1, h2, h3 {
    font-family: 'Helvetica Neue', sans-serif;
}

/* 响应式调整 */
@media (max-width: 768px) {
    .wp-block-columns {
        flex-direction: column;
    }
}

最佳实践建议

始终使用子主题：不要直接修改父主题，这样父主题更新时您的修改不会丢失
使用钩子函数：优先使用 add_action() 和 add_filter() 来修改功能
保持更新：定期检查父主题更新，确保兼容性
版本控制：使用 Git 管理您的子主题代码
性能优化：合理排队脚本和样式，避免重复加载

调试和测试

启用 WP_DEBUG 来检查错误
使用浏览器开发者工具检查样式
在不同设备和浏览器上测试响应式设计

官方文档参考

子主题开发指南
区块主题开发
theme.json 参考

完整子主题结构示例
twentytwentyfour-child/
├── style.css          # 必需：样式表
├── functions.php      # 必需：功能文件
├── screenshot.png     # 推荐：主题截图
├── theme.json         # 可选：区块主题配置
├── templates/         # 可选：模板文件
│   └── index.html
├── parts/             # 可选：模板部件
│   └── header.html
└── assets/            # 可选：静态资源
    ├── css/
    ├── js/
    └── images/

现在您已经可以开始创建和定制您的 Twenty Twenty-Four 子主题了。如果您在开发过程中遇到任何具体问题，比如需要修改特定模板或添加自定义功能，请随时继续提问！
注意：在修改任何文件之前，建议先备份您的网站，并在本地或测试环境中进行开发测试。
