Title: wp_list_categoriesの第三階層のcurrent-cat-parentについて
Last modified: 2013年6月26日

---

# wp_list_categoriesの第三階層のcurrent-cat-parentについて

 *  [hikonyan0127](https://ja.wordpress.org/support/users/hikonyan0127/)
 * (@hikonyan0127)
 * [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/)
 * こんにちは。
 * 現在、wp_list_categoriesを使用して
    下記のようにカスタム投稿のカテゴリーメニュー（
   3階層） をサイドに表示しています。
 * >  （例）
   >  ・やさい 　　・りんご 　　　　・青森県産 　　　　・京都産 　　・にんじん・
   > 東京産 　　　　・福岡県産 　　・かぼちゃ ・にく 　　・ぶた 　　　　・青森県
   > 産 　　　　・京都産 　　・とり 　　・うし
 * 該当のページを表示している時は
    他のメニューを表示しないようにしたかったので 「
   current-cat-parent」と「current-cat」を使用しCSSで設定しました。
 *     ```
       #main_menu li ul.children{ padding: 0; display:none; }
       #main_menu li ul.children li ul.children{ padding: 0; display:none;}
       #main_menu li ul.children li.current-cat-parent ul.children{display:block;}
       #main_menu li.current-cat-parent ul.children {display:block;}
       ```
   
 * 一見、うまくいっていたのですが、
    第3階層（青森県産など）をクリックしたときに、
   第一階層のやさいに「current-cat-parent」が付かなくなるため、非表示のままになって
   いることに気づきました。。。 (第二階層に付いてます)
 * 階層が深くなっても、第一階層にclassを付ける方法はないでしょうか？
    ご教授いただける
   と幸いです。

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

 *  [Takuro Hishikawa](https://ja.wordpress.org/support/users/hissy/)
 * (@hissy)
 * [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162676)
 * wp_list_categories は内部的に Walker_Category クラスを用いて各項目にクラス名の
   付与を行なっています。Walker_Category::start_el() の処理を追うと現在のカテゴリー
   の時に current-cat 直接の親の場合のみ current-cat-parent という属性値を設定し
   ていることが分かります
    [https://github.com/WordPress/WordPress/blob/3.5.2/wp-includes/category-template.php#L814](https://github.com/WordPress/WordPress/blob/3.5.2/wp-includes/category-template.php#L814)
 * で、Walker_Categoryクラスは継承してカスタマイズできますので、だれかやってない
   かなと思ってググったら見つけました。mago, kodomo, oyaという属性値を設定するサンプル
   です。
    [https://gist.github.com/tenman/2644185](https://gist.github.com/tenman/2644185)
 * ご参考までに。
 *  [Takuro Hishikawa](https://ja.wordpress.org/support/users/hissy/)
 * (@hissy)
 * [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162680)
 * ご参考までにで済ますのもどうかなと思ったので書いてみました。単に子カテゴリーを
   辿って現在表示しているカテゴリーがある場合の全てで “current-cat-ancestor” という
   属性値が付加されるようにする例です。継承元の Walker_Category::start_el() から
   変更している部分にコメントを入れています。ちょっと手を加えただけですね。
 * ▼テーマのfunctions.phpに追記
 *     ```
       class My_Walker_Category extends Walker_Category {
   
       	/**
       	 * @see Walker::start_el()
       	 * @since 2.1.0
       	 *
       	 * @param string $output Passed by reference. Used to append additional content.
       	 * @param object $category Category data object.
       	 * @param int $depth Depth of category in reference to parents.
       	 * @param array $args
       	 */
       	function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
       		extract($args);
   
       		$cat_name = esc_attr( $category->name );
       		$cat_name = apply_filters( 'list_cats', $cat_name, $category );
       		$link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
       		if ( $use_desc_for_title == 0 || empty($category->description) )
       			$link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
       		else
       			$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
       		$link .= '>';
       		$link .= $cat_name . '</a>';
   
       		if ( !empty($feed_image) || !empty($feed) ) {
       			$link .= ' ';
   
       			if ( empty($feed_image) )
       				$link .= '(';
   
       			$link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
   
       			if ( empty($feed) ) {
       				$alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
       			} else {
       				$title = ' title="' . $feed . '"';
       				$alt = ' alt="' . $feed . '"';
       				$name = $feed;
       				$link .= $title;
       			}
   
       			$link .= '>';
   
       			if ( empty($feed_image) )
       				$link .= $name;
       			else
       				$link .= "<img src='$feed_image'$alt$title" . ' />';
   
       			$link .= '</a>';
   
       			if ( empty($feed_image) )
       				$link .= ')';
       		}
   
       		if ( !empty($show_count) )
       			$link .= ' (' . intval($category->count) . ')';
   
       		if ( 'list' == $args['style'] ) {
       			$output .= "\t<li";
       			$class = 'cat-item cat-item-' . $category->term_id;
       			if ( !empty($current_category) ) {
       				$_current_category = get_term( $current_category, $category->taxonomy );
       				if ( $category->term_id == $current_category )
       					$class .=  ' current-cat';
       				elseif ( $category->term_id == $_current_category->parent )
       					$class .=  ' current-cat-parent';
       				// ここから追加
       				if ( in_array( $category->term_id, get_ancestors( $current_category, 'category' ) ) )
       					$class .= ' current-cat-ancestor';
       				// ここまで追加
       			}
       			$output .=  ' class="' . $class . '"';
       			$output .= ">$link\n";
       		} else {
       			$output .= "\t$link<br />\n";
       		}
       	}
   
       }
       ```
   
 * ▼テーマ側で
 *     ```
       <?php
       wp_list_categories( array( 'walker' => new My_Walker_Category ) );
       ?>
       ```
   
 *  トピック投稿者 [hikonyan0127](https://ja.wordpress.org/support/users/hikonyan0127/)
 * (@hikonyan0127)
 * [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162699)
 * ありがとうございます。
 * ここまで教えていただいたのに、カスタム投稿記事
    の全カテゴリーを出したい場合、
   taxonomyをどこで指定 すればよいかがわからず、止まっています。
 * 大変申し訳ないのですが
    ご教授いただけると幸いです。すみません。。
 *  [Takuro Hishikawa](https://ja.wordpress.org/support/users/hissy/)
 * (@hissy)
 * [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162711)
 * 「カスタム投稿記事の全カテゴリー」？
    ううむ、どういう状態かよく分かりません。
   wp_list_categories はもともとどのように書かれていたのですか？
 *  トピック投稿者 [hikonyan0127](https://ja.wordpress.org/support/users/hikonyan0127/)
 * (@hikonyan0127)
 * [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162724)
 * お手数おかけして、すみません。
    もともと下記のように、sidebar.phpに書いていま
   した。
 *     ```
       <ul id="main_menu">
       <?php wp_list_categories( 'title_li=&taxonomy=products_cat&orderby=order' ); ?>
       </ul>
       ```
   
 *  [Takuro Hishikawa](https://ja.wordpress.org/support/users/hissy/)
 * (@hissy)
 * [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162729)
 *     ```
       $args = array(
         'title_li' => '',
         'taxonomy' => 'products_cat',
         'orderby' => 'order',
         'walker' => new My_Walker_Category
       );
       wp_list_categories( $args );
       ```
   
 * My_Walker_Category の中でも ‘category’ と決め打ちで書いてるところがあったので
   修正
 *     ```
       class My_Walker_Category extends Walker_Category {
   
       	function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
       		extract($args);
   
       		$cat_name = esc_attr( $category->name );
       		$cat_name = apply_filters( 'list_cats', $cat_name, $category );
       		$link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
       		if ( $use_desc_for_title == 0 || empty($category->description) )
       			$link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
       		else
       			$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
       		$link .= '>';
       		$link .= $cat_name . '</a>';
   
       		if ( !empty($feed_image) || !empty($feed) ) {
       			$link .= ' ';
   
       			if ( empty($feed_image) )
       				$link .= '(';
   
       			$link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
   
       			if ( empty($feed) ) {
       				$alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
       			} else {
       				$title = ' title="' . $feed . '"';
       				$alt = ' alt="' . $feed . '"';
       				$name = $feed;
       				$link .= $title;
       			}
   
       			$link .= '>';
   
       			if ( empty($feed_image) )
       				$link .= $name;
       			else
       				$link .= "<img src='$feed_image'$alt$title" . ' />';
   
       			$link .= '</a>';
   
       			if ( empty($feed_image) )
       				$link .= ')';
       		}
   
       		if ( !empty($show_count) )
       			$link .= ' (' . intval($category->count) . ')';
   
       		if ( 'list' == $args['style'] ) {
       			$output .= "\t<li";
       			$class = 'cat-item cat-item-' . $category->term_id;
       			if ( !empty($current_category) ) {
       				$_current_category = get_term( $current_category, $category->taxonomy );
       				if ( $category->term_id == $current_category )
       					$class .=  ' current-cat';
       				elseif ( $category->term_id == $_current_category->parent )
       					$class .=  ' current-cat-parent';
       				// ここから追加
       				if ( in_array( $category->term_id, get_ancestors( $current_category, 'products_cat' ) ) )
       					$class .= ' current-cat-ancestor';
       				// ここまで追加
       			}
       			$output .=  ' class="' . $class . '"';
       			$output .= ">$link\n";
       		} else {
       			$output .= "\t$link<br />\n";
       		}
       	}
   
       }
       ```
   

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

 トピック「wp_list_categoriesの第三階層のcurrent-cat-parentについて」には新たに返信
することはできません。

## タグ

 * [current-cat](https://ja.wordpress.org/support/topic-tag/current-cat/)
 * [current-cat-parent](https://ja.wordpress.org/support/topic-tag/current-cat-parent/)
 * [wp_list_categories](https://ja.wordpress.org/support/topic-tag/wp_list_categories/)

 * フォーラム: [テーマ](https://ja.wordpress.org/support/forum/themes/)
 * 6件の返信
 * 2人の参加者
 * 最後の返信者: [Takuro Hishikawa](https://ja.wordpress.org/support/users/hissy/)
 * 最後の活動: [13年、 3ヶ月前](https://ja.wordpress.org/support/topic/wp_list_categories%e3%81%ae%e7%ac%ac%e4%b8%89%e9%9a%8e%e5%b1%a4%e3%81%aecurrent-cat-parent%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6/#post-162729)
 * 状態: 未解決

## トピック

### 人気のトピック

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

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

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

### 未解決のトピック

### すべてのトピック
