wp_get_theme()
テーマ情報取得の関数。テーマの style.css
に書かれているメタ情報を取得できる。
戻り値は WP_Theme オブジェクト。
例
wp_get_theme.php
<?php
$my_theme = wp_get_theme();
// Echo the name of the current active theme.
echo $my_theme; // wp_get_theme()
// Display the Current Theme's Version
echo $my_theme->Name . " is version " . $my_theme->Version;
// Display the Current Theme Author URI
echo $my_theme->{ 'Author URI' };
// Or,
echo $my_theme->get( 'AuthorURI' );
// Get Other Data: Text Domain & Theme URI
echo $my_theme->get( 'TextDomain' );
echo $my_theme->get( 'ThemeURI' );
で何が便利なの
テーマにスタイルシートや JavaScript ファイルを埋め込むには wp_enqueue_style()
あるいは wp_enqueue_script()
を使います。
- https://developer.wordpress.org/reference/functions/wp_enqueue_style/
- https://developer.wordpress.org/reference/functions/wp_enqueue_script/
どちらもバージョンの引数を指定できますが、指定しないと WordPress のバージョンが使われます。
まぁ、スタイルシートのテーマバージョンを更新した時にPHPも直すのが面倒って時に便利。
(なにゆえバージョンをちゃんと指定するかは公式テーマだったり、キャシュ関係とか)
という実装を Iemoto でしています。
https://github.com/megumiteam/iemoto/blob/master/root/functions.php#L110
(grunt-init テンプレートなのでよしなに置き換えてください)
git のバージョン番号とかにしたいってときは以下を参考に
https://memo.dogmap.jp/2015/02/27/wordpress-git-commit-id-to-theme-version/