Title: 特定の親カテゴリ以下を非表示にさせたい
Last modified: 2016年9月9日

---

# 特定の親カテゴリ以下を非表示にさせたい

 *  解決済 [sasakure_san](https://ja.wordpress.org/support/users/sasakure_san/)
 * (@sasakure_san)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/)
 * 記事内に、その記事に登録されているカテゴリを表示させたいと考えております。
    その
   際、特定のカテゴリは表示させたくありません。
 * [http://mizyupon-rpg.com/posts/285.html](http://mizyupon-rpg.com/posts/285.html)
 * 上記を参考に、カテゴリid=2を除外するよう下記のタグを設置しました。
 *     ```
       <?php
           $cats = get_the_category();
           $exclude = array(2);
           foreach((array)$cats as $cat){
               if(!in_array($cat->cat_ID, $exclude)){
                   echo '<a href="' . get_category_link($cat->cat_ID) . '">' . $cat->cat_name . '</a>';
               }
           } ?>
       ```
   
 * たしかにカテゴリid=2は非表示となったのですが、その子カテゴリは表示されてしまい
   ます。
    カテゴリid=2の親以下(子、孫)すべてを非表示にする方法はございますでしょう
   か。
 * お知恵をお貸しいただけますと幸いです。
    何卒よろしくお願い申し上げます。
    -  このトピックは[sasakure_san](https://ja.wordpress.org/support/users/sasakure_san/)
      が10年、 1ヶ月前に変更しました。

9件の返信を表示中 - 1 - 9件目 (全9件中)

 *  [oisit](https://ja.wordpress.org/support/users/oisit/)
 * (@oisit)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208693)
 * [関数リファレンス/get terms](https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_terms)
 *     ```
       $cats = get_the_category();
           $exclude = array(2);
       ```
   
 * の代わりにget termsと引数にexclude_treeとか使うんでしょうか。
 *  [천궁 메텔](https://ja.wordpress.org/support/users/digitmaetel/)
 * (@digitmaetel)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208694)
 * 属しているカテゴリーの親だけでいいんですよね
 * get_termsの引数にparent=0を追加することで、親カテゴリーだけを指定することができ
   ます。
 *  トピック投稿者 [sasakure_san](https://ja.wordpress.org/support/users/sasakure_san/)
 * (@sasakure_san)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208702)
 * oisit様
    digitmaetel様 早速のご返信誠にありがとうございます。
 * > get_termsの引数にparent=0を追加することで、親カテゴリーだけを指定することが
   できます。
 * PHP初心者のため的外れなことを言っていましたら申し訳ございません。
    自分なりに
   調べてみたのですが、下記のようなコードとなりますでしょうか。
 *     ```
       <?php $categories = get_the_terms( $post->ID, 'category', array(
       	'parent' => 0,
       ));
       ?>
       ```
   
 *  トピック投稿者 [sasakure_san](https://ja.wordpress.org/support/users/sasakure_san/)
 * (@sasakure_san)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208705)
 * 再度こちらで調べてコードを作成しました。
 *     ```
       <?php
       $taxonomies =  'category';
       $args = array(
       	'exclude_tree' => array(2)
       	); 
       $terms = get_terms($taxonomies, $args);
       if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
           foreach ( $terms as $term ) {
               echo '<li><a href="' . get_term_link( $term ) .'">' . $term->name .'</a></li>';
           }
       }
       ?>
       ```
   
 * 狙い通りカテゴリid=2の親以下は表示されなくなりましたが
    それ以外のカテゴリがすべて
   表示されてしまいます…。
 * そこで再度ご相談ですが、現在の記事ページのカテゴリのみ
    表示するにはどうすれば
   よいでしょうか。
 * 素人で申し訳ございません。
    お知恵を拝借できますと幸いです。
 *  [oisit](https://ja.wordpress.org/support/users/oisit/)
 * (@oisit)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208749)
 * あまりレスが付きませんね。get_termsじゃないのでしょうか。
    効率化いいのか悪い
   のかわかりませんが、 get_the_categoryでinclude用のカテゴリー配列を作って、指定
   してやるあるいはforeachの段階でifでincludeにあるカテゴリーとかするんでしょうか。
 *  [kimipooh](https://ja.wordpress.org/support/users/kimipooh/)
 * (@kimipooh)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208759)
 * 孫まで限定なら下記の通りかなぁと思いますが、さらに奥深くまでやるなら再帰呼び出し
   が必要かなーと思います。
    そのあたりはちょっと試している時間がないので割愛。
 * 投稿のカテゴリー IDの 2　とその子と孫カテゴリーを非表示にするには
    $exclude =
   array(2); に子と孫カテゴリーIDを探して追加したらよいということですよね。その部分
   のみが下記のコードです。
 *     ```
       $parent_id = 2;
       $exclude = array($parent_id);
       $term_children =  get_term_children( $parent_id, 'category'); // 親ID 2　の子カテゴリーをチェックし、そのIDを配列に保存
   
       if($term_children){  // もし１つでも小カテゴリーがあれば、、、
         array_merge ($exclude, $term_children); // 子カテゴリーIDを $exclude に追記
   
         // 各子カテゴリー内に孫カテゴリーがあるかチェック
         foreach ($term_children as $child){
           $term = get_term_by ('id', $child, 'category'); 
           if($term){ 
              $exclude[] = $term->term_id; // 孫カテゴリーがあれば、追記
           }
         }
       }
       ```
   
 * これで $exclude配列に、親ID 2　の子カテゴリーIDと孫カテゴリーIDが入っているはず
   ですよ。
 *  [kimipooh](https://ja.wordpress.org/support/users/kimipooh/)
 * (@kimipooh)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208760)
 * おっと、get_terms を使えば簡単みたいですね。
 *     ```
       $parent_id = 2;
       $exclude = array($parent_id);
       $term_children =  get_terms( 'category', array('child_of' => $parent_id) );
   
       if($term_children){
        foreach($term_children as $child){
           $exclude[] = $child->term_id;
        }
       }
       ```
   
 * で奥深いところまで全部探してくれますね。
 *  [kimipooh](https://ja.wordpress.org/support/users/kimipooh/)
 * (@kimipooh)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208761)
 * つまりは、下記のようなコードになるのかなと思います。
 *     ```
       <?php
           $cats = get_the_category();
   
           $parent_id = 2;
           $exclude = array($parent_id);
           $term_children =  get_terms( 'category', array('child_of' => $parent_id) );
           if($term_children){
             foreach($term_children as $child){
               $exclude[] = $child->term_id;
             }
           }
   
          foreach((array)$cats as $cat){
               if(!in_array($cat->cat_ID, $exclude)){
                   echo '<a href="' . get_category_link($cat->cat_ID) . '">' . $cat->cat_name . '</a>';
               }
           } ?>
       ```
   
 *  トピック投稿者 [sasakure_san](https://ja.wordpress.org/support/users/sasakure_san/)
 * (@sasakure_san)
 * [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208773)
 * oisit様
    kimipooh様
 * この度は色々とご指導いただき、誠にありがとうございます。
    kimipooh様よりご提示
   いただきましたコードを埋め込むことで 無事、表示することができました！
 *     ```
       <?php
           $cats = get_the_category();
       	$parent_id = 2;
           $exclude = array($parent_id);
           $term_children =  get_terms( 'category', array('child_of' => $parent_id) );
           	if($term_children){
       			foreach($term_children as $child){
           		$exclude[] = $child->term_id;
        			}
        		};
           foreach((array)$cats as $cat){
               if(!in_array($cat->cat_ID, $exclude)){
                   echo '<li><a href="' . get_category_link($cat->cat_ID) . '">' . $cat->cat_name . '</a></li>';
               }
           } ?>
       ```
   
 * 長々と初心者にお付き合いいただき、本当に感謝しております。
    また何かありましたら
   ご指導いただけますと幸いです。
 * この度は本当にありがとうございました！

9件の返信を表示中 - 1 - 9件目 (全9件中)

 トピック「特定の親カテゴリ以下を非表示にさせたい」には新たに返信することはできま
せん。

## タグ

 * [カテゴリ　非表示　除外](https://ja.wordpress.org/support/topic-tag/%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e3%80%80%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%80%80%e9%99%a4%e5%a4%96/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9件の返信
 * 4人の参加者
 * 最後の返信者: [sasakure_san](https://ja.wordpress.org/support/users/sasakure_san/)
 * 最後の活動: [10年、 1ヶ月前](https://ja.wordpress.org/support/topic/%e7%89%b9%e5%ae%9a%e3%81%ae%e8%a6%aa%e3%82%ab%e3%83%86%e3%82%b4%e3%83%aa%e4%bb%a5%e4%b8%8b%e3%82%92%e9%9d%9e%e8%a1%a8%e7%a4%ba%e3%81%ab%e3%81%95%e3%81%9b%e3%81%9f%e3%81%84/#post-208773)
 * 状態: 解決済

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
