为了避免主题更新后造成主题文件(例如functions.php / header.php / footer.php等)写的自定义代码丢失,建议从一开始就创建子主题,这样自定义内容才会保留在子主题中。下面以Astra主题为例:
一、基础配置
1.在themes文件夹下创建子主题文件夹 /wp-content/themes/astra-child
2.新增functions.php文件,继承Astra主题的配置(必须)
<?php
/**
* Astra Child Theme functions and definitions
*/
add_action( 'wp_enqueue_scripts', 'astra_child_enqueue_styles' );
function astra_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')
);
}
3.新增style.css文件(必须)
/*
Theme Name: Astra Child
Theme URI: https://wpastra.com/
Description: Astra的子主题
Template: astra
Version: 1.0.0
Text Domain: astra-child
*/
4.新增screenshot.png图片,作为后台查看主题的展示封面
5.启用子主题
- 进入 WordPress 后台 → 外观 → 主题,找到 Astra Child 并启用
- 到这里就已经完成子主题的创建及安装啦,子主题会继承所有 Astra 的原有样式,后台主题自定义中的配置(额外CSS、页眉、页脚、排版、布局、菜单等)也会被继承,已遇到的bug有页脚的配置不完整
6.注意
- 如果是在linux上添加文件,需要更改文件权限为777,才能在wp后台主题文件编辑器中修改主题代码
切换为root用户: sudo -i
递归修改文件夹及文件权限: chmod -R 777 /path/to/your/folder/
单独修改文件权限: chmod 777 /path/to/your/file
查看文件夹权限: ls -ld /path/to/your/folder/
查看文件夹内文件权限: ls -l /path/to/your/folder/
查看文件权限: ls -l /path/to/your/file
- 如果直接修改了父主题的PHP文件(如header.php),这些修改不会自动继承到子主题,应该将这些修改转移到子主题的对应文件中
- 如果在子主题中创建了同名模板文件,会优先使用子主题的文件,而完全忽略父主题的同名文件,不会进行合并
二、添加测试文件检查子主题是否正常
在子主题中添加test.php文件,然后启动站点,直接访问测试文件,查看页面显示的结果
<?php
/**
* 测试文件 - 验证子主题是否正确加载
* 访问此文件可以检查子主题的状态
* 站点地址/wp-content/themes/astra-child/test.php
*/
// 检查是否直接访问
if (!defined('ABSPATH')) {
define('ABSPATH', dirname(dirname(dirname(dirname(__FILE__)))) . '/');
}
// 加载WordPress
require_once(ABSPATH . 'wp-load.php');
// 检查当前主题
$current_theme = wp_get_theme();
$parent_theme = $current_theme->parent();
echo '<h1>子主题测试页面</h1>';
echo '<h2>当前主题信息</h2>';
echo '<p><strong>当前主题:</strong> ' . $current_theme->get('Name') . ' (' . $current_theme->get_stylesheet() . ')</p>';
echo '<p><strong>父主题:</strong> ' . ($parent_theme ? $parent_theme->get('Name') : '无') . '</p>';
echo '<p><strong>主题目录:</strong> ' . get_stylesheet_directory() . '</p>';
// 检查主题激活状态
$active_theme = get_option('stylesheet');
$template_theme = get_option('template');
echo '<p><strong>激活的主题 (stylesheet):</strong> ' . $active_theme . '</p>';
echo '<p><strong>模板主题 (template):</strong> ' . $template_theme . '</p>';
// 检查function.php文件
$function_file = get_stylesheet_directory() . '/functions.php';
echo '<p><strong>functions.php文件:</strong> ' . (file_exists($function_file) ? '存在' : '不存在') . '</p>';
if (file_exists($function_file)) {
echo '<p><strong>functions.php文件大小:</strong> ' . filesize($function_file) . ' 字节</p>';
echo '<p><strong>functions.php最后修改时间:</strong> ' . date('Y-m-d H:i:s', filemtime($function_file)) . '</p>';
}
// 检查hook是否被注册
echo '<h2>Hook 检查</h2>';
echo '<p><strong>astra_head_top:</strong> ' . (has_action('astra_head_top') ? '已注册' : '未注册') . '</p>';
echo '<p><strong>astra_footer_after:</strong> ' . (has_action('astra_footer_after') ? '已注册' : '未注册') . '</p>';
// 检查函数是否存在
echo '<h2>函数检查</h2>';
echo '<p><strong>add_google_gtag_header:</strong> ' . (function_exists('add_google_gtag_header') ? '存在' : '不存在') . '</p>';
echo '<p><strong>add_floating_contact_buttons:</strong> ' . (function_exists('add_floating_contact_buttons') ? '存在' : '不存在') . '</p>';
// 检查样式文件
echo '<h2>样式文件检查</h2>';
$child_style = get_stylesheet_directory() . '/style.css';
echo '<p><strong>子主题样式文件:</strong> ' . (file_exists($child_style) ? '存在' : '不存在') . '</p>';
if (file_exists($child_style)) {
$style_content = file_get_contents($child_style);
echo '<p><strong>样式文件内容:</strong></p>';
echo '<pre>' . htmlspecialchars($style_content) . '</pre>';
}
// 检查WordPress是否加载了子主题
echo '<h2>WordPress加载检查</h2>';
echo '<p><strong>get_template_directory():</strong> ' . get_template_directory() . '</p>';
echo '<p><strong>get_stylesheet_directory():</strong> ' . get_stylesheet_directory() . '</p>';
echo '<p><strong>get_template_directory_uri():</strong> ' . get_template_directory_uri() . '</p>';
echo '<p><strong>get_stylesheet_directory_uri():</strong> ' . get_stylesheet_directory_uri() . '</p>';
echo '<h2>调试信息</h2>';
echo '<p>如果你看到这个页面,说明子主题已经正确加载。</p>';
echo '<p>请检查你的WordPress管理后台 -> 外观 -> 主题,确保Astra Child主题已激活。</p>';
echo '<p>如果问题仍然存在,请清除缓存并刷新页面。</p>';
// 尝试手动加载functions.php
echo '<h2>手动加载测试</h2>';
if (file_exists($function_file)) {
echo '<p>尝试手动加载functions.php...</p>';
try {
include_once($function_file);
echo '<p>✅ functions.php已手动加载</p>';
// 再次检查函数
echo '<p><strong>手动加载后 - add_google_gtag_header:</strong> ' . (function_exists('add_google_gtag_header') ? '存在' : '不存在') . '</p>';
echo '<p><strong>手动加载后 - add_floating_contact_buttons:</strong> ' . (function_exists('add_floating_contact_buttons') ? '存在' : '不存在') . '</p>';
} catch (Exception $e) {
echo '<p>❌ 手动加载失败: ' . $e->getMessage() . '</p>';
}
} else {
echo '<p>❌ functions.php文件不存在</p>';
}
?>