syuichi-tsuji
2/8/2013 - 5:11 PM

[WordPress] wp_nav_menu のクラスに独自ルールを追加する

[WordPress] wp_nav_menu のクラスに独自ルールを追加する

<?php
function nav_menu_class_customize($sorted_menu_items) {
	foreach ($sorted_menu_items as $sorted_menu_item) {
		// if menu item is a post type object (is not an archive or custom)
		if ($sorted_menu_item->type == 'post_type') {
			// get post object from object id
			$post_obj = get_post($sorted_menu_item->object_id);
			
			// rules
			if ($post_obj->post_name == 'hoge') {
				$sorted_menu_item->classes[] = 'this-page-is-hoge';
			}
		}
	}
	return $sorted_menu_items;
}

add_filter('wp_nav_menu_objects','nav_menu_class_customize');