Twenty-Elvenにやったけど、もう一度復習
1. 親テーマと同じ階層に子テーマフォルダーを作る
フォルダー名 ex.child_twentysixteen
2. 今作ったフォルダーchild_twentysixteenの中に
style.cssを作り、
/*
Template:twentysixteen
Theme Name:child_twentysixteen
*/
と記述。
以前は
WordPressの子テーマ化という方法でテンプレートをカスタマイズする
のように
style.cssに
@import url(‘../twentysixteen/style.css’);
と記述するとスタイルシートが読み込まれたが(今でも読み込まれる)が、もはや非推奨。
3. functions.phpを作り、子テーマのstyle.cssを読み込む記述をする。
書き込む場所は
functions.phpの最初の行の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' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array('parent-style')
);
この時、
親テーマのfunctions.phpのすべての内容を子テーマのfunctions.phpにコピーしないこと
なぜかというと、子テーマの functions.php が最初に読み込まれ、次に親テーマのfunctions.phpが読み込まれるから。だから、子テーマのfunctions.phpの関数は脱着可能であり、条件付きで定義することにより、子テーマから置換可能にできる。