wc_nav_menu_item_classes

Fix active class in nav for shop page.

Description

(array) wc_nav_menu_item_classes( (array) $menu_items ); 

Returns (array)

Parameters (1)

0. $menu_items (array)
The menu items.

Usage

  1. if ( !function_exists( 'wc_nav_menu_item_classes' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-page-functions.php'; 
  3.  
  4. // The menu items. 
  5. $menu_items = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_nav_menu_item_classes($menu_items); 
  9.  

Defined (1)

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

/includes/wc-page-functions.php  
  1. function wc_nav_menu_item_classes( $menu_items ) { 
  2.  
  3. if ( ! is_woocommerce() ) { 
  4. return $menu_items; 
  5.  
  6. $shop_page = (int) wc_get_page_id( 'shop' ); 
  7. $page_for_posts = (int) get_option( 'page_for_posts' ); 
  8.  
  9. foreach ( (array) $menu_items as $key => $menu_item ) { 
  10.  
  11. $classes = (array) $menu_item->classes; 
  12.  
  13. // Unset active class for blog page 
  14. if ( $page_for_posts == $menu_item->object_id ) { 
  15. $menu_items[ $key ]->current = false; 
  16.  
  17. if ( in_array( 'current_page_parent', $classes ) ) { 
  18. unset( $classes[ array_search( 'current_page_parent', $classes ) ] ); 
  19.  
  20. if ( in_array( 'current-menu-item', $classes ) ) { 
  21. unset( $classes[ array_search( 'current-menu-item', $classes ) ] ); 
  22.  
  23. // Set active state if this is the shop page link 
  24. } elseif ( is_shop() && $shop_page == $menu_item->object_id && 'page' === $menu_item->object ) { 
  25. $menu_items[ $key ]->current = true; 
  26. $classes[] = 'current-menu-item'; 
  27. $classes[] = 'current_page_item'; 
  28.  
  29. // Set parent state if this is a product page 
  30. } elseif ( is_singular( 'product' ) && $shop_page == $menu_item->object_id ) { 
  31. $classes[] = 'current_page_parent'; 
  32.  
  33. $menu_items[ $key ]->classes = array_unique( $classes ); 
  34.  
  35.  
  36. return $menu_items;