_wp_menu_item_classes_by_context

Add the class property classes for the current context, if applicable.

Description

_wp_menu_item_classes_by_context( (array) &$menu_items ); 

Parameters (1)

0. $menu_items (array) => &$menu_items
The current menu item objects to which to add the class property information.

Usage

  1. if ( !function_exists( '_wp_menu_item_classes_by_context' ) ) { 
  2. require_once ABSPATH . WPINC . '/nav-menu-template.php'; 
  3.  
  4. // The current menu item objects to which to add the class property information. 
  5. $menu_items = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = _wp_menu_item_classes_by_context($menu_items); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/nav-menu-template.php  
  1. function _wp_menu_item_classes_by_context( &$menu_items ) { 
  2. global $wp_query, $wp_rewrite; 
  3.  
  4. $queried_object = $wp_query->get_queried_object(); 
  5. $queried_object_id = (int) $wp_query->queried_object_id; 
  6.  
  7. $active_object = ''; 
  8. $active_ancestor_item_ids = array(); 
  9. $active_parent_item_ids = array(); 
  10. $active_parent_object_ids = array(); 
  11. $possible_taxonomy_ancestors = array(); 
  12. $possible_object_parents = array(); 
  13. $home_page_id = (int) get_option( 'page_for_posts' ); 
  14.  
  15. if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) { 
  16. foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) { 
  17. if ( is_taxonomy_hierarchical( $taxonomy ) ) { 
  18. $term_hierarchy = _get_term_hierarchy( $taxonomy ); 
  19. $terms = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) ); 
  20. if ( is_array( $terms ) ) { 
  21. $possible_object_parents = array_merge( $possible_object_parents, $terms ); 
  22. $term_to_ancestor = array(); 
  23. foreach ( (array) $term_hierarchy as $anc => $descs ) { 
  24. foreach ( (array) $descs as $desc ) 
  25. $term_to_ancestor[ $desc ] = $anc; 
  26.  
  27. foreach ( $terms as $desc ) { 
  28. do { 
  29. $possible_taxonomy_ancestors[ $taxonomy ][] = $desc; 
  30. if ( isset( $term_to_ancestor[ $desc ] ) ) { 
  31. $_desc = $term_to_ancestor[ $desc ]; 
  32. unset( $term_to_ancestor[ $desc ] ); 
  33. $desc = $_desc; 
  34. } else { 
  35. $desc = 0; 
  36. } while ( ! empty( $desc ) ); 
  37. } elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) { 
  38. $term_hierarchy = _get_term_hierarchy( $queried_object->taxonomy ); 
  39. $term_to_ancestor = array(); 
  40. foreach ( (array) $term_hierarchy as $anc => $descs ) { 
  41. foreach ( (array) $descs as $desc ) 
  42. $term_to_ancestor[ $desc ] = $anc; 
  43. $desc = $queried_object->term_id; 
  44. do { 
  45. $possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc; 
  46. if ( isset( $term_to_ancestor[ $desc ] ) ) { 
  47. $_desc = $term_to_ancestor[ $desc ]; 
  48. unset( $term_to_ancestor[ $desc ] ); 
  49. $desc = $_desc; 
  50. } else { 
  51. $desc = 0; 
  52. } while ( ! empty( $desc ) ); 
  53.  
  54. $possible_object_parents = array_filter( $possible_object_parents ); 
  55.  
  56. $front_page_url = home_url(); 
  57. $front_page_id = (int) get_option( 'page_on_front' ); 
  58.  
  59. foreach ( (array) $menu_items as $key => $menu_item ) { 
  60.  
  61. $menu_items[$key]->current = false; 
  62.  
  63. $classes = (array) $menu_item->classes; 
  64. $classes[] = 'menu-item'; 
  65. $classes[] = 'menu-item-type-' . $menu_item->type; 
  66. $classes[] = 'menu-item-object-' . $menu_item->object; 
  67.  
  68. // This menu item is set as the 'Front Page'. 
  69. if ( 'post_type' === $menu_item->type && $front_page_id === (int) $menu_item->object_id ) { 
  70. $classes[] = 'menu-item-home'; 
  71.  
  72. // if the menu item corresponds to a taxonomy term for the currently-queried non-hierarchical post object 
  73. if ( $wp_query->is_singular && 'taxonomy' == $menu_item->type && in_array( $menu_item->object_id, $possible_object_parents ) ) { 
  74. $active_parent_object_ids[] = (int) $menu_item->object_id; 
  75. $active_parent_item_ids[] = (int) $menu_item->db_id; 
  76. $active_object = $queried_object->post_type; 
  77.  
  78. // if the menu item corresponds to the currently-queried post or taxonomy object 
  79. } elseif ( 
  80. $menu_item->object_id == $queried_object_id && 
  81. ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && $wp_query->is_home && $home_page_id == $menu_item->object_id ) || 
  82. ( 'post_type' == $menu_item->type && $wp_query->is_singular ) || 
  83. ( 'taxonomy' == $menu_item->type && ( $wp_query->is_category || $wp_query->is_tag || $wp_query->is_tax ) && $queried_object->taxonomy == $menu_item->object ) 
  84. ) { 
  85. $classes[] = 'current-menu-item'; 
  86. $menu_items[$key]->current = true; 
  87. $_anc_id = (int) $menu_item->db_id; 
  88.  
  89. while( 
  90. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && 
  91. ! in_array( $_anc_id, $active_ancestor_item_ids ) 
  92. ) { 
  93. $active_ancestor_item_ids[] = $_anc_id; 
  94.  
  95. if ( 'post_type' == $menu_item->type && 'page' == $menu_item->object ) { 
  96. // Back compat classes for pages to match wp_page_menu() 
  97. $classes[] = 'page_item'; 
  98. $classes[] = 'page-item-' . $menu_item->object_id; 
  99. $classes[] = 'current_page_item'; 
  100.  
  101. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 
  102. $active_parent_object_ids[] = (int) $menu_item->post_parent; 
  103. $active_object = $menu_item->object; 
  104.  
  105. // if the menu item corresponds to the currently-queried post type archive 
  106. } elseif ( 
  107. 'post_type_archive' == $menu_item->type && 
  108. is_post_type_archive( array( $menu_item->object ) ) 
  109. ) { 
  110. $classes[] = 'current-menu-item'; 
  111. $menu_items[$key]->current = true; 
  112. // if the menu item corresponds to the currently-requested URL 
  113. } elseif ( 'custom' == $menu_item->object && isset( $_SERVER['HTTP_HOST'] ) ) { 
  114. $_root_relative_current = untrailingslashit( $_SERVER['REQUEST_URI'] ); 
  115. $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current ); 
  116. $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url; 
  117. $item_url = set_url_scheme( untrailingslashit( $raw_item_url ) ); 
  118. $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) ); 
  119.  
  120. if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) { 
  121. $classes[] = 'current-menu-item'; 
  122. $menu_items[$key]->current = true; 
  123. $_anc_id = (int) $menu_item->db_id; 
  124.  
  125. while( 
  126. ( $_anc_id = get_post_meta( $_anc_id, '_menu_item_menu_item_parent', true ) ) && 
  127. ! in_array( $_anc_id, $active_ancestor_item_ids ) 
  128. ) { 
  129. $active_ancestor_item_ids[] = $_anc_id; 
  130.  
  131. if ( in_array( home_url(), array( untrailingslashit( $current_url ), untrailingslashit( $_indexless_current ) ) ) ) { 
  132. // Back compat for home link to match wp_page_menu() 
  133. $classes[] = 'current_page_item'; 
  134. $active_parent_item_ids[] = (int) $menu_item->menu_item_parent; 
  135. $active_parent_object_ids[] = (int) $menu_item->post_parent; 
  136. $active_object = $menu_item->object; 
  137.  
  138. // give front page item current-menu-item class when extra query arguments involved 
  139. } elseif ( $item_url == $front_page_url && is_front_page() ) { 
  140. $classes[] = 'current-menu-item'; 
  141.  
  142. if ( untrailingslashit($item_url) == home_url() ) 
  143. $classes[] = 'menu-item-home'; 
  144.  
  145. // back-compat with wp_page_menu: add "current_page_parent" to static home page link for any non-page query 
  146. if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id ) 
  147. $classes[] = 'current_page_parent'; 
  148.  
  149. $menu_items[$key]->classes = array_unique( $classes ); 
  150. $active_ancestor_item_ids = array_filter( array_unique( $active_ancestor_item_ids ) ); 
  151. $active_parent_item_ids = array_filter( array_unique( $active_parent_item_ids ) ); 
  152. $active_parent_object_ids = array_filter( array_unique( $active_parent_object_ids ) ); 
  153.  
  154. // set parent's class 
  155. foreach ( (array) $menu_items as $key => $parent_item ) { 
  156. $classes = (array) $parent_item->classes; 
  157. $menu_items[$key]->current_item_ancestor = false; 
  158. $menu_items[$key]->current_item_parent = false; 
  159.  
  160. if ( 
  161. isset( $parent_item->type ) && 
  162. // ancestral post object 
  163. 'post_type' == $parent_item->type && 
  164. ! empty( $queried_object->post_type ) && 
  165. is_post_type_hierarchical( $queried_object->post_type ) && 
  166. in_array( $parent_item->object_id, $queried_object->ancestors ) && 
  167. $parent_item->object != $queried_object->ID 
  168. ) || 
  169.  
  170. // ancestral term 
  171. 'taxonomy' == $parent_item->type && 
  172. isset( $possible_taxonomy_ancestors[ $parent_item->object ] ) && 
  173. in_array( $parent_item->object_id, $possible_taxonomy_ancestors[ $parent_item->object ] ) && 
  174. ! isset( $queried_object->term_id ) || 
  175. $parent_item->object_id != $queried_object->term_id 
  176. ) { 
  177. $classes[] = empty( $queried_object->taxonomy ) ? 'current-' . $queried_object->post_type . '-ancestor' : 'current-' . $queried_object->taxonomy . '-ancestor'; 
  178.  
  179. if ( in_array( intval( $parent_item->db_id ), $active_ancestor_item_ids ) ) { 
  180. $classes[] = 'current-menu-ancestor'; 
  181. $menu_items[$key]->current_item_ancestor = true; 
  182. if ( in_array( $parent_item->db_id, $active_parent_item_ids ) ) { 
  183. $classes[] = 'current-menu-parent'; 
  184. $menu_items[$key]->current_item_parent = true; 
  185. if ( in_array( $parent_item->object_id, $active_parent_object_ids ) ) 
  186. $classes[] = 'current-' . $active_object . '-parent'; 
  187.  
  188. if ( 'post_type' == $parent_item->type && 'page' == $parent_item->object ) { 
  189. // Back compat classes for pages to match wp_page_menu() 
  190. if ( in_array('current-menu-parent', $classes) ) 
  191. $classes[] = 'current_page_parent'; 
  192. if ( in_array('current-menu-ancestor', $classes) ) 
  193. $classes[] = 'current_page_ancestor'; 
  194.  
  195. $menu_items[$key]->classes = array_unique( $classes );