修改 WordPress 主题时,不改动原始主题,而是修改备份(即子主题)中的文件并使用这些文件的方法。
- 为了便于识别所用的子主题,请组合主题名称,在 “wp-content/themes” 文件夹中创建一个新文件夹。(例如:如果原始主题是 “twentyfifteen”,则创建名为 “twentyfifteen-child” 的文件夹)
- 将原始主题的 “style.css” 复制到子主题文件夹,并按如下所示修改 “Theme Name” 和 “Themplate”。
/* Theme Name: Twenty Fifteen Child Theme URI: https://wordpress.org/themes/twentyfifteen/ Template: twentyfifteen Author: the WordPress team Author URI: https://wordpress.org/ Description: Our 2015 default theme is clean, blog-focused, and designed for clarity. Twenty Fifteen's simple, straightforward typography is readable on a wide variety of screen sizes, and suitable for multiple languages. We designed it using a mobile-first approach, meaning your content takes center-stage, regardless of whether your visitors arrive by smartphone, tablet, laptop, or desktop computer. Version: 1.5 License: GNU General Public License v2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html Tags: black, blue, gray, pink, purple, white, yellow, dark, light, two-columns, left-sidebar, fixed-layout, responsive-layout, accessibility-ready, custom-background, custom-colors, custom-header, custom-menu, editor-style, featured-images, microformats, post-formats, rtl-language-support, sticky-post, threaded-comments, translation-ready Text Domain: twentyfifteenTemplate: twntyfifteen This theme, like WordPress, is licensed under the GPL. Use it to make something cool, have fun, and share what you've learned with others. */ - 在子主题文件夹中创建 “functions.php”,并写入以下代码。
<?php add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } ?> - 将原始主题的 “screenshot.png” 复制到子主题文件夹。
- 将要修改的原始主题文件复制到子主题文件夹中,修改后即可使用。
(参考:http://codex.wordpress.org/Child_Themes)