【发布时间】:2018-04-16 18:33:46
【问题描述】:
我已经开始研究 wordpress 主题,需要将其设为子主题,以防止更新更改我的网站和自定义。
有什么我可以做到的吗?
【问题讨论】:
-
wordpress.stackexchange.com 可能会更好地为您服务
标签: wordpress themes parent-child
我已经开始研究 wordpress 主题,需要将其设为子主题,以防止更新更改我的网站和自定义。
有什么我可以做到的吗?
【问题讨论】:
标签: wordpress themes parent-child
您只需要遵循以下 3 个步骤:
1) 在 /wp-content/themes/ 中为您的主题创建一个文件夹:
“建议(虽然不是必需的,特别是如果您正在创建一个供公共使用的主题)您的子主题目录的名称附加“-child”。”。
您在此处创建的每个文件都会覆盖您父主题中的文件。例如,您可以创建新的页面模板:https://developer.wordpress.org/themes/template-files-section/page-template-files/ 或覆盖现有的。
2) 创建一个 style.css 文件并以:
开头/*
Theme Name: Twenty Fifteen Child
Theme URI: http://example.com/twenty-fifteen-child/
Description: Twenty Fifteen Child Theme
Author: John Doe
Author URI: http://example.com
Template: twentyfifteen
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
Text Domain: twenty-fifteen-child
*/
在此处添加您的自定义样式。更多信息:https://codex.wordpress.org/Theme_Development#Theme_Stylesheet
3) 创建一个functions.php
在此文件中,您可以为您的子主题添加新功能或修改现有功能。
您还必须将父主题的 CSS 和 JS 以及您在子主题上创建的新 CSS 和 JS 加入队列。大部分时间都是这样的:
<?php
function my_theme_enqueue_styles() {
$parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
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', 'my_theme_enqueue_styles' );
?>
现在您可以添加自定义简码 (https://codex.wordpress.org/Shortcode_API) 、小部件区域 (https://codex.wordpress.org/Widgetizing_Themes) 等。
更多关于子主题的文档:https://codex.wordpress.org/Child_Themes
【讨论】: