カスタム投稿タイプ毎にリンク色や、タグ色を変更したい、、みたいなときにbodyタグにclassを追加したくなったときの備忘。
functions.phpの中で、
function posttype_class( $classes ) {
global $post;
$post_type_class = "post-type-" . $post->post_type;
$classes[] = $post_type_class;
return $classes;
}
add_filter( 'body_class', 'posttype_class' );
上のように、body_classに対するフィルタを追加してあげると、bodyタグのclass指定の最後にpost-type-xxxというclassが追加されます。
あとは、cssで post-type-xxxを親にした記述を追加してあげる投稿タイプ毎に一括でスタイルを変更できます。
.post-type-xxx a {
background-color: #f00;
}
ついでに、”wp_enqueue_scripts” アクションを追加してあげて、、post-xxx.cssというような別ファイルを読み込ませると、変更しているところが一覧化できて見やすくなったりします。
function my_styles() {
wp_enqueue_style( 'xxx-style', get_bloginfo('stylesheet_directory') . '/post-xxx.css', array(), null, 'all');
}
add_action( 'wp_enqueue_scripts', 'my_styles');