[WordPress]フォルダー、URLを取得

設定条件

以下の設定でサイトを運用していると仮定。

サイトのURL:https://sample.com
wpのインストールフォルダー:/home/www/sample/wp/
親テーマ:/home/www/sample/wp/wp-content/themes/oya/
子テーマ:/home/www/sample/wp/wp-content/themes/co/

フォルダー

定数・関数動作戻り値や動作など備考
ABSPATHWordPressのインストール先/home/www/sample/wp/(定数)
get_stylesheet_directory()子テーマ(style.css)のフォルダー/home/www/sample/wp/wp-content/themes/co‘/’なし
get_template_directory()親テーマのフォルダー/home/www/sample/wp/wp-content/themes/oya‘/’なし
get_template_part(‘inc/aaa’, ‘bbb’);テーマ内PHPファイルのインクルード‘/home/www/sample/wp/wp-content/themes/co/inc/aaa-bbb.php’ ファイルをインクルードする
 
 
 

URL

定数・関数動作戻り値備考
get_stylesheet_directory_uri()子テーマ(style.css)のURLhttps://sample.com/wp/wp-content/themes/co‘/’なし
home_url($path, $scheme)
$path, $schemeは省略可
ホーム URL を取得https://sample.com‘/’なし
$pathに’/’を指定するとよい
site_url( $path, $scheme )
$path, $schemeは省略可
WordPressを設置したURLを取得https://sample.com/wp‘/’なし
 

確認用ソース

// フォルダーやURLを確認するショートコード
add_shortcode('dirinfo', function () {
    $info = '';
    $info .= "ABSPATH : [" . ABSPATH . "]";
    $info .= "get_stylesheet_directory() : [" . get_stylesheet_directory() . "]";
    $info .= "get_template_directory() : [" . get_template_directory() . "]";
    $info .= "get_stylesheet_directory_uri() : [" . get_stylesheet_directory_uri() . "]";

    return $info;
});