漫川web
2716 字
9 分钟
创建和制作WordPress主题的子主题
一、了解子主题的作用
二、准备工作
三、创建子主题的样式表(style.css)
/*
Theme Name: 父主题名称 子主题(如 Twenty Twenty-Three Child)
Theme URI: 父主题官网链接(如 https://wordpress.org/themes/twentytwentythree/)
Description: 子主题描述(如 “Child theme for Twenty Twenty-Three”)
Author: 你的姓名或网站
Author URI: 你的网站链接
Template: 父主题文件夹名称(如 twentytwentythree)
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: 子主题文本域(建议与文件夹名称一致,如 twentytwentythree-child)
*/
/* 在这里添加自定义 CSS 样式,会覆盖父主题样式 */
body {
font-size: 18px;
color: #333;
}
四、创建子主题的 functions.php(可选)
若需通过代码扩展功能(如添加自定义函数、修改父主题功能),可在子主题文件夹中创建 functions.php
文件。
注意: 子主题的 functions.php
会覆盖父主题的同名文件,但会先加载父主题的 functions.php
。
示例代码(以添加菜单功能为例):
<?php
// 确保父主题的 functions.php 已加载
add_action( 'after_setup_theme', 'child_theme_setup' );
function child_theme_setup() {
// 继承父主题的主题设置(可选)
require_once get_template_directory() . '/functions.php';
// 添加自定义功能(如注册菜单)
register_nav_menus( array(
'header-menu' => __( 'Header Menu', 'twentytwentythree-child' ),
) );
}
// 添加自定义 CSS 样式表(可选,推荐直接在 style.css 中编写)
function child_theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri(),
array( 'parent-style' ),
wp_get_theme()->get('Version') // 确保子主题样式随版本更新
);
}
add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_styles' );
?>
这个思路66的