get_parent_theme_file_path()


WordPressのget_parent_theme_file_path()関数は、親テーマのファイルパスを取得するために使用されます。この関数は、子テーマを使用している場合でも、親テーマのファイルパスを正確に取得することができます。

構文

get_parent_theme_file_path( string $file );
  • $file(string) — 親テーマ内のファイルのパスを指定します。この引数はオプションで、指定しない場合は親テーマのディレクトリパスが返されます。

例1: 親テーマのスタイルシートのパスを取得

親テーマのスタイルシートのパスを取得する例です。

<?php $stylesheet_path = get_parent_theme_file_path('style.css'); ?>

例2: 親テーマのfunctions.phpファイルのパスを取得

親テーマのfunctions.phpファイルのパスを取得する例です。

<?php $functions_path = get_parent_theme_file_path('functions.php'); ?>

例3: 親テーマのディレクトリパスを取得

親テーマのディレクトリパスを取得する例です。

<?php $theme_dir = get_parent_theme_file_path(); ?>

例4: 親テーマの画像ファイルのパスを取得

親テーマのimagesディレクトリ内の画像ファイルのパスを取得する例です。

<?php $image_path = get_parent_theme_file_path('images/logo.png'); ?>

例5: 親テーマのJavaScriptファイルのパスを取得

親テーマのjsディレクトリ内のJavaScriptファイルのパスを取得する例です。

<?php $js_path = get_parent_theme_file_path('js/script.js'); ?>

例6: 親テーマのテンプレートファイルのパスを取得

親テーマのテンプレートファイルのパスを取得する例です。

<?php $template_path = get_parent_theme_file_path('templates/single.php'); ?>

例7: 親テーマの言語ファイルのパスを取得

親テーマのlanguagesディレクトリ内の言語ファイルのパスを取得する例です。

<?php $language_path = get_parent_theme_file_path('languages/ja.mo'); ?>

例8: 親テーマのカスタムファイルのパスを取得

親テーマ内のカスタムファイルのパスを取得する例です。

<?php $custom_path = get_parent_theme_file_path('inc/custom-functions.php'); ?>

注意点

  • この関数は、子テーマを使用している場合でも、親テーマのファイルパスを取得するために使用されます。
  • ファイルが存在しない場合でも、パスは返されます。ファイルの存在を確認するには、file_exists()関数を使用してください。

関連機能: