静的なHTMLサイトではbodyにidやclassを設定して違うcssをあてる方法をよく使う。それをWordPressでもやりたい。
シンプルな実装方法
<body id="<?php echo esc_attr( $post->post_name ); ?>" <?php body_class(); ?>>
この方法だとfunctions.phpなので余計な記述をしなくて良いのでスッキリ!
いままでやっていた方法の一例
<?php //bodyにID設定
$customPostTypeObj = get_post_type_object(get_post_type())->name;
$category = get_the_category();
$postName = $post->post_name;
$body_id ="";
if ( is_front_page() ) {
$body_id = 'id="'.top.'"';
} else if ( is_page() ) {
$body_id = 'id="'.$postName.'"';
} else if ( is_category() ) {
$body_id = 'id="category_'.$category[0]->category_nicename.'"';
} else if ( is_post_type_archive() ) {
$body_id = 'id="'.$customPostTypeObj.'"';
}
?>
<body <?php echo $body_id; ?> <?php if(is_front_page()): ?><?php else: ?><?php body_class('pgs'); ?><?php endif; ?>>