WordPress Snippet

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

MyWalker extends Walker_Category

depth 0 をulから、divに置き換える

functions.php

class MyWalker extends Walker_Category {

	function start_el(&$output, $category, $depth, $args) {
		extract($args);

		$cat_name= esc_attr( $category->name );

		$link= '<a href="%1$s" rel="%2$s">%3$s</a>';

		$link= sprintf($link
							, esc_attr( get_term_link($category) )
							, $category->slug
							, $cat_name
							);
		if ( !empty($show_count) ){
			$link .= ' (' . intval($category->count) . ')';
		}
		if($category->category_parent > 0 ){

			$output .= '<li class="'.$class.'">'.$link;
		}
		if($category->category_parent== 0){
			$output .= '<div><h1>'.$cat_name.'</h1>';
		}	

	}

	function end_el( &$output, $page, $depth= 0, $args= array() ) {
		if ( 'list' != $args['style'] )
			return;

		if($depth > 0){
			$output .= "</div></li>\n";
		}else{
			$output .= "\n";

		}
	}

}

Your template

$MyWalker= new MyWalker();
$args= array('walker'=> $MyWalker,'echo'=> 0,'show_count'=> 1);
$result= str_replace(array( '<li class="categories">カテゴリー<ul>'),'',wp_list_categories($args));
echo rtrim( $result,'</ul></li>');

[emulsion_relate_posts]