投稿に、投稿メタデータを扱うカスタムフィールドがあるように、テーマに関しても、そのテーマごとのメタデータを扱う必要がある
例えば、twentytenでは、背景やヘッダー画像ヘッダテキスト色などがそれにあたる
これらのレコードは、options テーブルに mods_テーマファイル名 というフィールドに保存される
利用方法のサンプルは以下のようになる
.footer {
border-top: solid 1px #<?php echo get_theme_mod( 'background_color' ); ?>}
}
取得、設定、削除は、以下の関数を利用できる。
類似のもので、以下のような関数がありますが
<?php add_option($name, $value= '', $deprecated= '', $autoload= 'yes'); ?>
複数必要な場合、同じテーマファイル用のoptionテーブルのフィールドが複数になるのに対して、この関数は、ひとつのフィールドに複数のメタ情報を格納する点が異なる。
$nameには、テーマ名が入る
<?php set_theme_mod( $name, $value ) ?> <?php remove_theme_mod( $name ) ?> Remove theme modifications option for current theme. <?php remove_theme_mods() ?>
get_theme_mod($name, $default= false)
※get_theme_modは、returnの時に、 return apply_filters( “theme_mod_$name”, $mods[$name] ), 第二引数は、見つからなかったときのデフォルト値
theme_modで現在なにがセットされているのかを調べる方法
$mods= get_theme_mods(); var_dump($mods);
style.cssのページヘッダ情報の取得
function my_current_theme_info() {
$themes= get_themes();
$current_theme= get_current_theme();
if ( ! isset( $themes[$current_theme] ) ) {
delete_option( 'current_theme' );
$current_theme= get_current_theme();
}
$ct->name= $current_theme;
$ct->title= $themes[$current_theme]['Title'];
$ct->version= $themes[$current_theme]['Version'];
$ct->parent_theme= $themes[$current_theme]['Parent Theme'];
$ct->template_dir= $themes[$current_theme]['Template Dir'];
$ct->stylesheet_dir= $themes[$current_theme]['Stylesheet Dir'];
$ct->template= $themes[$current_theme]['Template'];
$ct->stylesheet= $themes[$current_theme]['Stylesheet'];
$ct->screenshot= $themes[$current_theme]['Screenshot'];
$ct->description= $themes[$current_theme]['Description'];
$ct->author= $themes[$current_theme]['Author'];
$ct->tags= $themes[$current_theme]['Tags'];
$ct->theme_root= $themes[$current_theme]['Theme Root'];
$ct->theme_root_uri= $themes[$current_theme]['Theme Root URI'];
return $ct;
}
$theme= my_current_theme_info();
echo '<pre>';
var_dump( $theme );
echo '</pre>';