Created
July 13, 2016 04:46
-
-
Save mypacecreator/18636bc350238586033f2fdba3958095 to your computer and use it in GitHub Desktop.
body_classに固定ページのルート、投稿タイプのスラッグ、カテゴリースラッグを追加する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function add_page_root_body_class( $classes ) { | |
| $post_type = get_query_var( 'post_type' ); | |
| if ( is_page() ) { | |
| global $post; | |
| if ( $post->ancestors ) { | |
| $root = $post->ancestors[count($post->ancestors) - 1]; | |
| $root_post = get_post( $root ); | |
| $classes[] = esc_attr( $root_post->post_name ); | |
| } else { | |
| $classes[] = esc_attr( $post->post_name ); | |
| } | |
| } elseif ( $post_type ) { | |
| global $post; | |
| $classes[] = esc_attr( $post_type ); | |
| } elseif ( is_single() ) { | |
| $cat = get_the_category(); | |
| $cat_slug = $cat[0]->category_nicename; | |
| $classes[] = esc_attr( $cat_slug ); | |
| } elseif ( is_category() ) { | |
| $cat_id = get_query_var('cat'); | |
| $cat = get_category($cat_id); | |
| $cat_slug = $cat->category_nicename; | |
| $classes[] = esc_attr( $cat_slug ); | |
| } | |
| return $classes; | |
| } | |
| add_filter( 'body_class', 'add_page_root_body_class' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment