WordPress「add_theme_support」でカスタム背景を有効化する

WordPressのカスタム背景を有効化すると、カスタマイザーから背景色を変更できるようになったり、背景画像を設定できるようになる。

カスタム背景の有効化

body_class()wp_head()を配置した上で、functions.phpにカスタム背景を有効化するコードを記述する。


// カスタム背景
add_theme_support('custom-background');

外観>カスタマイズの左パネルに、背景画像が表示されるようになる。

左パネル

色の設定

背景画像の設定

出力されるスタイル

1bodyタグにcustom-backgroundというクラス名が付与される。


<body id="page-top" class="home blog custom-background">

2headタグにインラインでスタイルが追加される。


<style type="text/css" id="custom-background-css">
  body.custom-background { background-color: #dd3333; }
</style>

初期値を設定する


$args = array(
	'default-color' => '000000',
	'default-image' => '%1$s/images/background.jpg',
);
add_theme_support( 'custom-background', $args );

Related Tags