WordPressでbodyにクラスを追加する方法

bodyにbody_classを使用すると、ページごとに異なるclassを追加でき、
ページごとにスタイルを変えることができます。
基本的な使い方
header.php のbody開始タグを以下の通りに書き変えます。
|
1 |
<body <?php body_class(); ?>> |
※デフォルトテンプレートにははじめからbody_classが記述されています。
オリジナルテンプレートを作成する際に記述しておくと便利です。
【出力例】
・トップページ
|
1 |
<body class="home blog"> |
・記事詳細ページ
|
1 |
<body class="post-template-default single single-post postid-3842 single-format-standard"> |
・固定ページ
|
1 |
<body class="page-template-default page page-id-2"> |
任意のclass名を追加
|
1 |
<body <?php body_class('class01'); ?>> |
【出力例】
|
1 |
<body class="home blog class01"> |
複数のclass名を追加する場合は、class名を半角スペースで区切って指定します。
|
1 |
<body <?php body_class('class01 class02'); ?>> |
【出力例】
|
1 |
<body class="home blog class01 class02"> |
![MARKLEAPS[マークリープス]](https://markleaps.com/blog/wp-content/themes/mkl/images/00_logo.png)