親カテゴリ 分岐 

※なんかうまくいかなかった

ファンクションへ
タクソノミーだけなら下の部分だけでOK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* For categories */
if ( ! function_exists( 'post_is_in_descendant_category' ) ) :
function post_is_in_descendant_category( $cats, $_post = null ) {
  foreach ( (array) $cats as $cat ) {
        // get_term_children() accepts integer ID only
        $descendants = get_term_children( (int) $cat, 'category' );
        if ( $descendants && in_category( $descendants, $_post ) )
            return true;
    }
    return false;
}
function post_is_in_category_slug($slug){
    global $post;
    $post_id = ( isset($post->ID) ) ? $post->ID : '';
    if ( in_category( $slug, $post_id ) || post_is_in_descendant_category( get_term_by('slug',$slug,'category'), $post_id ) ){
        return true;
    } else {
        return false;
    }
}
endif; //function_exists 'post_is_in_descendant_category'
  
  
/* For taxonomies */
if ( ! function_exists( 'post_is_in_descendant_taxonomy' ) ) :
function post_is_in_descendant_taxonomy( $terms, $taxonomy, $_post = null ) {
    foreach ( (array) $terms as $term ) {
        // get_term_children() accepts integer ID only
        $descendants = get_term_children( (int) $term, $taxonomy );
        if ( $descendants && has_term( $descendants, $taxonomy, $_post ) )
            return true;
    }
    return false;
}
function post_is_in_taxonomy_slug($slug, $taxonomy){
    global $post;
    $post_id = ( isset($post->ID) ) ? $post->ID : '';
    if ( has_term( $slug, $taxonomy, $post_id ) || post_is_in_descendant_taxonomy( get_term_by('slug',$slug,$taxonomy), $taxonomy, $post_id ) ){
        return true;
    } else {
        return false;
    }
}
endif; //function_exists 'post_is_in_descendant_taxonomy'
1
2
3
4
5
6
7
8
9
10
11
//宅
if ( post_is_in_taxonomy_slug('Cat-A', カスタムタクソノミー名) )
//具体例
<?php
  if (post_is_in_taxonomy_slug('ホームページ制作', cate_jirei) ):
?>
<?php endif; ?>
 
//カテ
 
if ( post_is_in_category_slug('Cat-A')