WordPress Snippet

ワードプレスをカスタマイズしよう

wp_dropdown_categoriesでoptgroupを表示

function end_lvlに、deps check を追加

<?php
//http://wordpress.org/support/topic/walker_categorydropdown-with?replies=2

class Walker_CategoryDropdownCustom extends Walker_CategoryDropdown {
	function start_el( $output, $category, $depth, $args) {
		global $wp_query;
		$cat_name= apply_filters( 'list_cats', $category->name, $category );

		if ($depth== 0) {
			$output .= "\t<optgroup class=\"level-$depth\" value=\"".$category->term_id."\"";
			$output .= ' label="'.$cat_name.'">';
		} else {
			$output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
			$output .= '>';
			$output .= $cat_name;
			$output .= "</option>\n";
		}
	}
	function end_lvl( $output, $depth, $args) {
		if( $depth==0 ) {
			$output .= "</optgroup>\n";
		}
	}
}
$instance= new Walker_CategoryDropdownCustom;
wp_dropdown_categories( array('hierarchical'=> true,'walker'=> $instance) );

?>

http://ja.forums.wordpress.org/topic/58092?replies=2#post-104849(b:@source)

[emulsion_relate_posts]