My開発メモ

子テーマを作成する (WordPress)

WordPressで既存のテーマを使ってサイトをつくるとき、 子テーマを作って修正・変更を加えると、テーマのアップデート があったとき、対応しやすい。

1) 子テーマのフォルダを作成する

たとえば、親テーマが「parent-theme」だったら、同じ階層に 「parent-theme-child」というふうにフォルダを作成する。

2) スタイルシートを作成する

style.css を作成し、以下の内容を記入する。

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
*/

Theme Name と Template は絶対必要。

3) functions.php

functions.php を作成し、以下の内容とする。

functions.php

<?php
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'));
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
?>
4) 子テーマを有効化する
5) 変更したいファイルを子テーマのフォルダにコピーして修正する

たとえば、header.php を修正したければ、header.php を子テーマのフォルダにコピーして、修正する。

子テーマにあるファイルが優先されるようになっている。

6) favicon.ico 生成

Real Favicon Generator

参考サイト

カテゴリー: memo, wordpress

タグ:

カウント: 183