2026年7月27日

2026年7月27日

WordPressの子テーマエラーを解決する方法

はじめに

WordPressで子テーマを作成して有効化しようとしたが「壊れたテーマ」と表示される・子テーマを有効化したのに見た目が親テーマと全く同じで変更が反映されない・子テーマのfunctions.phpに書いたコードが動作しない・子テーマのCSSが親テーマのスタイルを上書きできないといった問題は、子テーマの必須ファイルと設定を正しく理解することで解決できます。

症状・原因

  • style.cssTemplate:ヘッダーが親テーマのフォルダ名と一致していない(最も多いミス)
  • 子テーマのディレクトリにstyle.cssが存在しない、またはファイルパーミッションが正しくない
  • 子テーマのfunctions.phpで親テーマのCSSを読み込む処理を書いていないため、親テーマのスタイルが消える
  • 子テーマと親テーマのfunctions.phpが競合しており、同名関数が再定義エラーを起こしている

解決手順

ステップ1:子テーマエラーを診断する

# 現在有効なテーマを確認
wp theme list --status=active

# 子テーマの style.css の Template: を確認
wp eval "
\$theme = wp_get_theme();
echo '現在のテーマ: ' . \$theme->get('Name') . PHP_EOL;
echo 'Template: ' . \$theme->get('Template') . PHP_EOL;
echo '親テーマ: ' . (\$theme->parent() ? \$theme->parent()->get('Name') : 'なし') . PHP_EOL;
"

# 親テーマのフォルダ名を確認(Template: と一致させる必要あり)
wp eval "
foreach (wp_get_themes() as \$slug => \$theme) {
    echo \$slug . ' → ' . \$theme->get('Name') . PHP_EOL;
}
"

# 子テーマのファイル構成を確認
wp eval "
\$child_dir = get_stylesheet_directory();
echo '子テーマディレクトリ: ' . \$child_dir . PHP_EOL;
echo 'style.css: ' . (file_exists(\$child_dir . '/style.css') ? '✓' : '× なし') . PHP_EOL;
echo 'functions.php: ' . (file_exists(\$child_dir . '/functions.php') ? '✓' : '× なし') . PHP_EOL;
"

ステップ2:子テーマの必須ファイルを正しく作成する

/* style.css(子テーマの最低限必要な内容) */

/*
 Theme Name:   My Child Theme
 Theme URI:    https://example.com/my-child-theme/
 Description:  Twenty Twenty-Four の子テーマ
 Author:       Your Name
 Template:     twentytwentyfour
 Version:      1.0.0
 Text Domain:  my-child-theme
*/

/*
 ↑ Template: には親テーマのフォルダ名を記述(wp-content/themes/のフォルダ名)
   例: twentytwentyfour, hello-elementor, storefront
   スペルミスや大文字/小文字の違いで子テーマと認識されない
*/

/* 子テーマ独自のスタイルをここに追記 */
:root {
    --color-primary: #e74c3c;
}

.site-header {
    background-color: var(--color-primary);
}
<?php
// functions.php(子テーマ)

// ✅ 親テーマと子テーマの CSS を正しく読み込む
add_action('wp_enqueue_scripts', function(): void {
    // ✅ 親テーマの style.css を読み込む
    wp_enqueue_style(
        'parent-style',
        get_template_directory_uri() . '/style.css',  // 親テーマのパス
        [],
        wp_get_theme()->parent()->get('Version')
    );

    // ✅ 子テーマの style.css を親の後に読み込む
    wp_enqueue_style(
        'child-style',
        get_stylesheet_uri(),                          // 子テーマの style.css
        ['parent-style'],                              // 親の後に読み込む
        wp_get_theme()->get('Version')
    );
});

ステップ3:子テーマでテンプレートファイルを上書きする

子テーマのテンプレート上書きルール:
- 子テーマのテンプレートは親テーマより優先される
- 同名のファイルを子テーマに配置するだけで上書きできる

例)親テーマの single.php を上書きしたい場合:
  my-child-theme/
  ├── style.css
  ├── functions.php
  └── single.php   ← このファイルが優先される(親の single.php より先に検索される)

✅ 部分的な上書きには get_template_part() を使う:
  子テーマ: template-parts/content.php
  → 親テーマの template-parts/content.php より優先される
# 子テーマと親テーマの両方にあるテンプレートを確認
wp eval "
\$child  = get_stylesheet_directory();
\$parent = get_template_directory();
\$templates = ['single.php', 'page.php', 'archive.php', 'index.php'];
foreach (\$templates as \$tpl) {
    \$in_child  = file_exists(\$child  . '/' . \$tpl);
    \$in_parent = file_exists(\$parent . '/' . \$tpl);
    echo \$tpl . ': '
        . (\$in_child  ? '子テーマ ✓ ' : '子テーマ × ')
        . (\$in_parent ? '親テーマ ✓'  : '親テーマ ×')
        . PHP_EOL;
}
"

ステップ4:子テーマのfunctions.phpで親テーマの関数を上書きする

// ✅ 親テーマの function_exists チェックを利用した上書き
// 親テーマが以下のように書いている場合は子テーマで再定義できる:
// if (!function_exists('parent_theme_setup')) { function parent_theme_setup() {...} }

// 子テーマの functions.php:
function parent_theme_setup(): void {
    // 親テーマの after_setup_theme 処理を完全に置き換える
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    // 子テーマ独自の設定...
}

// ✅ フックを使って親テーマの処理を変更(フィルター/アクション)
// 親テーマのフックをより高い優先度で上書き
add_filter('excerpt_length', function(): int {
    return 80; // 親テーマが設定した文字数を上書き
}, 999);

// ✅ 親テーマのアクションを削除して差し替え
add_action('after_setup_theme', function(): void {
    // 親テーマの after_setup_theme を削除(優先度が同じ場合は注意)
    remove_action('after_setup_theme', 'parent_theme_setup');

    // 子テーマ独自の設定
    add_theme_support('custom-logo');
}, 11); // 親テーマより後に実行

ステップ5:子テーマのデバッグとよくある問題の解決

# 子テーマで PHP エラーが発生しているか確認
wp eval "
error_reporting(E_ALL);
\$child_functions = get_stylesheet_directory() . '/functions.php';
if (file_exists(\$child_functions)) {
    // 構文エラーチェック
    \$output = shell_exec('php -l ' . escapeshellarg(\$child_functions));
    echo \$output;
}
"

# 子テーマの WP_DEBUG ログを確認
wp eval "
\$log = WP_CONTENT_DIR . '/debug.log';
if (file_exists(\$log)) {
    \$lines = array_slice(file(\$log), -20);
    echo implode('', \$lines);
} else {
    echo 'debug.log なし(WP_DEBUG_LOG が無効)';
}
"
// ✅ 子テーマのよくある問題と解決策

// 問題1: 親テーマのCSSが読み込まれない
// → wp_enqueue_scripts で parent-style を明示的にエンキュー(上記ステップ2参照)

// 問題2: 子テーマの functions.php が動かない
// → <?php タグが正しいか・BOMなしUTF-8で保存されているか確認

// 問題3: 子テーマのテンプレートが読み込まれない
// → get_stylesheet_directory() と get_template_directory() の違いを確認
//   子テーマ: get_stylesheet_directory()
//   親テーマ: get_template_directory()

// 問題4: 親テーマのアップデートで変更が消える
// → 直接親テーマを編集している → 子テーマで上書きする方法に変更

// ✅ 子テーマと親テーマのパスを確認
add_action('wp_footer', function(): void {
    if (!current_user_can('manage_options')) {
        return;
    }
    echo '<!-- Child: ' . get_stylesheet_directory() . ' -->';
    echo '<!-- Parent: ' . get_template_directory() . ' -->';
});

注意事項

  • style.cssTemplate:には親テーマのフォルダ名wp-content/themes/以下のディレクトリ名)を記述してください。テーマ名(Theme Name:の値)ではありません。例えば「Twenty Twenty-Four」の場合はTemplate: twentytwentyfourとなります
  • 子テーマのfunctions.phpは親テーマのfunctions.phpより先に実行されます。親テーマの関数をremove_actionで削除する場合は優先度に注意してください

まとめ

WordPressの子テーマエラーの解決は①wp theme listで有効テーマ確認・style.cssTemplate:が親テーマフォルダ名と一致しているか確認、②style.cssTemplate:ヘッダーを正確に記述・functions.phpwp_enqueue_scriptsフックで親→子の順にCSSを読み込む、③同名テンプレートファイルを子テーマに配置するだけで上書き可能・get_stylesheet_directory()get_template_directory()の使い分け、④function_exists()チェックで親テーマ関数を上書き・フックの優先度(priority:11)で親テーマより後に実行・remove_actionで親テーマの処理を差し替え、⑤PHP構文チェック・デバッグログ確認・wp_footerでパス表示デバッグの手順で解決します。

お気軽にご相談ください

お見積りへ お問い合わせへ