WordPressで現在のテーマディレクトリまでのURLを取得する方法
このページでは、WordPressで現在のテーマディレクトリまでのURLを取得する方法について説明していきます。
get_template_directory_uri()関数
現在のテーマディレクトリまでのURLを取得するには、get_template_directory_uri()関数を用います。
get_template_directory_uri()関数の詳しい内容については、こちらのページ(英語)でもご覧いただくことができます。
それでは、以下で具体的な使用例を見ていきたいと思います。
使用例
PHP
<figure class="demo__pic">
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/common/WordPress.png" alt="" />
</figure>
この例では、imgタグのsrc属性を指定するためにget_template_directory_uri()関数を用いています。
結果
<figure class="demo__pic">
<img src="http://localhost/yagi-system/wp-content/themes/yagi-system/assets/images/common/WordPress.png" alt="">
</figure>

まとめ
このように、get_template_directory_uri()関数を用いることで、現在のテーマディレクトリまでもURLを取得することができます。