body_class()
参照:WordPress Codex-body_class()
構文
body_class($class[オプション])
body_class()の詳細
動 作
文字列のクラス属性を出力。
返り値
なし
引 数
$class[オプション]
(文字列又は配列)初期値:null
初期値以外に追加したいclass名。
複数の場合は、スペースで区切る。
body_class()を使ったコード例
実装
<body <?php body_class(); ?> <!--html出力 single.phpの場合--> <body class="single single-post postid-1">
ページごとにclass属性を設け、CSSを効率化
WordPressでサイトを構築する際、予めCSSを決めておきます。
その際、各ページによりclassの値を知っていると便利なので一覧にしました。
body_class()で出力されるクラス名:初期値
| テンプレートの種類 | クラス名 | 
|---|---|
| front-page.php | home blog | 
| front-page.php 固定ページを指定 | home page page-id-(ID) | 
| single.php | single single-post postid-(ID) | 
| page.php | page page-id-(ID) | 
| category.php | archive category category-(スラッグ) caetecory-(ID) | 
| tag.php | archive tag tag-(スラッグ) tag-(ID) | 
| date.php | archive date | 
| author.php | archive author author-(ナイスネーム) author-(ID) | 
| search.php | search search-results | 
| 404.php | error404 | 
body_class()のコード
tags/5.3/src/wp-includes/post-template.php
<?php /** * Displays the class names for the body element. * * @since 2.8.0 * * @param string|string[] $class Space-separated string or array of class names to add to the class list. */ function body_class( $class = '' ) { // Separates class names with a single space, collates class names for body element. echo 'class="' . join( ' ', get_body_class( $class ) ) . '"'; } ?>
 
 
		