wp_get_theme_data_custom_templates()


WordPressのwp_get_theme_data_custom_templates()関数は、テーマのカスタムテンプレートデータを取得するために使用されます。この関数は、テーマのtheme.jsonファイルやその他の設定ファイルからカスタムテンプレート情報を取得します。

構文

wp_get_theme_data_custom_templates( string $theme_slug );
  • $theme_slug(string) — カスタムテンプレートデータを取得したいテーマのスラッグを指定します。

例1: テーマのカスタムテンプレートデータを取得

この例では、指定したテーマのカスタムテンプレートデータを取得します。

$custom_templates = wp_get_theme_data_custom_templates('twentytwentyone');

例2: カスタムテンプレートデータを表示

取得したカスタムテンプレートデータを表示します。

print_r(wp_get_theme_data_custom_templates('twentytwentyone'));

例3: 特定のカスタムテンプレートを取得

特定のカスタムテンプレートのデータを取得します。

$template = wp_get_theme_data_custom_templates('twentytwentyone')['single-post'];

例4: カスタムテンプレートの存在を確認

カスタムテンプレートが存在するかどうかを確認します。

if(isset(wp_get_theme_data_custom_templates('twentytwentyone')['single-post'])) { echo '存在します'; }

例5: カスタムテンプレートのリストを取得

カスタムテンプレートのリストを取得します。

$templates = array_keys(wp_get_theme_data_custom_templates('twentytwentyone'));

例6: カスタムテンプレートの詳細を表示

カスタムテンプレートの詳細情報を表示します。

echo wp_get_theme_data_custom_templates('twentytwentyone')['single-post']['description'];

例7: カスタムテンプレートのファイルパスを取得

カスタムテンプレートのファイルパスを取得します。

echo wp_get_theme_data_custom_templates('twentytwentyone')['single-post']['file'];

注意点

  • この関数は、テーマのtheme.jsonファイルが正しく設定されている必要があります。
  • 指定したテーマスラッグが存在しない場合、空の配列が返されます。
  • カスタムテンプレートが定義されていない場合、該当するデータは返されません。

関連機能: