サポート » テーマ » wp_list_categoriesの第三階層のcurrent-cat-parentについて

  • こんにちは。

    現在、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

    (@hissy)

    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

    で、Walker_Categoryクラスは継承してカスタマイズできますので、だれかやってないかなと思ってググったら見つけました。mago, kodomo, oyaという属性値を設定するサンプルです。
    https://gist.github.com/tenman/2644185

    ご参考までに。

    モデレーター Takuro Hishikawa

    (@hissy)

    ご参考までにで済ますのもどうかなと思ったので書いてみました。単に子カテゴリーを辿って現在表示しているカテゴリーがある場合の全てで “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

    (@hikonyan0127)

    ありがとうございます。

    ここまで教えていただいたのに、カスタム投稿記事
    の全カテゴリーを出したい場合、taxonomyをどこで指定
    すればよいかがわからず、止まっています。

    大変申し訳ないのですが
    ご教授いただけると幸いです。すみません。。

    モデレーター Takuro Hishikawa

    (@hissy)

    「カスタム投稿記事の全カテゴリー」?
    ううむ、どういう状態かよく分かりません。
    wp_list_categories はもともとどのように書かれていたのですか?

    トピック投稿者 hikonyan0127

    (@hikonyan0127)

    お手数おかけして、すみません。
    もともと下記のように、sidebar.phpに書いていました。

    <ul id="main_menu">
    <?php wp_list_categories( 'title_li=&taxonomy=products_cat&orderby=order' ); ?>
    </ul>
    モデレーター Takuro Hishikawa

    (@hissy)

    $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について」には新たに返信することはできません。