首页 问答 正文

如何创建wordpress主题的子主题?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 15 次

创建wordpress主题的子主题可以参照以下步骤:

  1. 创建新的文件夹
    在主题的目录下新建一个文件夹,文件夹名可以任意,例如“child-theme”。

  2. 创建style.css文件
    在新建的文件夹中创建一个style.css文件,并在文件开头添加以下代码:

/*
Theme Name: Child Theme
Theme URI: https://example.com/
Description: Child theme of Parent Theme
Author: Your Name
Author URI: https://example.com/
Template: parent-theme-name
Version: 1.0.0
*/ 

其中Template后面的“parent-theme-name”指定为需要创建子主题的父主题的名称。

  1. 创建functions.php文件
    在子主题的文件夹中新建一个functions.php文件,并在文件开头添加以下代码:
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
    $parent_style = 'parent-style';
    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')
    );
} 

这段代码会将子主题的样式文件(style.css)加载到网站中,并且确保它是父主题样式文件(style.css)的子主题。

  1. 上传到网站
    将子主题文件夹上传到网站的/wp-contents/themes/目录下即可。

  2. 激活子主题
    在WordPress的后台中,选择外观 – 主题,激活子主题即可。

以上就是创建wordpress主题的子主题的步骤。

大家谈论
    我的见解