@Satoh_D no blog

大分にUターンしたので記念に。調べたこととか作ったこととか食べたこととか

WordPressで子テーマのディレクトリのパスを出力する方法

今回携わった案件で、初めて子テーマ機能を使用したのですが、
子テーマのディレクトリのパスを出力する際にハマったことがあったのでメモ。

子テーマ内でのbloginfo('template_url'); は非推奨らしい

子テーマのディレクトリにあるファイルを指定しようといつも通りに /hogehoge/... と書いたのですが、何故か返ってくるのは親テーマのパス。
おかしいなーおかしいなーと唸っていたらTwitterで「その記述は非推奨」との指摘が。
改めて「Function Reference/bloginfo « WordPress Codex」のページを見てみると思いっきり書いてました。

'template_url' / 'template_directory' - URL of the active theme's directory ('template_directory' was a local path before 2.6; see get_theme_root() and get_template() for hackish alternatives.) Within child themes, both get_bloginfo('template_url') and get_template() will return the parent theme directory. Consider echoing get_template_directory_uri() instead (for the parent template directory) or get_stylesheet_directory_uri() (for the child template directory).

どうやら子テーマの中で bloginfo('template_url'); は親テーマのディレクトリを返すから、子テーマのディレクトリのパスを出力するには get_stylesheet_directory_uri() 使用しなければいけないみたいです。
因みに子テーマから親テーマのディレクトリのパスを出力する時は get_template_directory_uri() を使用しなければいけません。

ちゃんと仕様に書いていましたね...。
ハマった時は仕様をちゃんと読むべきだと痛感しました。