sevenphp / wordpress-themesample-child
这是一个WordPress子主题骨架示例,展示了如何按照最佳实践实现子主题。
dev-master
2016-11-04 13:34 UTC
Requires
- composer/installers: ~1.0
This package is auto-updated.
Last update: 2024-08-29 04:46:42 UTC
README
这是当我构建子主题时的一个示例指南。
参考
步骤
-
确保将子主题目录追加为 '-child'
-
应至少包含以下两个文件
- style.css - functions.php
-
样式表必须从标题开始
/* Theme Name: Parent Name Child Theme URI: [] Description: A Child Theme of [Parent Name] Author: Wasseem Khayrattee Author URI: http://khayrattee.com Template: parent-folder-name # should corresponds to the directory name of the parent theme Version: a-number--optional General comments/License Statement if any. */
- 子主题的 style.css 将始终直接覆盖其父主题的对应部分
-
需要 functions.php 来正确地排入样式。不要在 style.css 中使用 @import() 来导入父主题样式表。这是不好的实践。所以使用排入。
<?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' ); ?>
覆盖父主题的函数.php中的内容
- 子主题的 functions.php 不会 覆盖其父主题的对应部分
- 它是在父主题的函数.php之后加载的
- 它是在父主题的函数.php之后加载的
注意
-
我们可以通过在子主题中首先包含一个函数来利用上述内容,以便覆盖父主题中的对应函数,通过 条件声明函数 来实现。
if ( ! function_exists( 'theme_special_nav' ) ) { function theme_special_nav() { // Do something. } }
get_stylesheet_directory()
当我们需要获取子主题当前目录路径时使用此方法。这是因为WordPress总是给出当前活动的主题路径。
示例
require_once( get_stylesheet_directory() . '/my_included_file.php' );
其他
有关 RTL 支持和 本地化 的信息,请参阅 https://codex.wordpress.org/Child_Themes