Skip to content

Instantly share code, notes, and snippets.

@dannydickson
Created October 19, 2019 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannydickson/1348b8cc909723cf41de74fc48a0e9fb to your computer and use it in GitHub Desktop.
Save dannydickson/1348b8cc909723cf41de74fc48a0e9fb to your computer and use it in GitHub Desktop.
Add dynamic ACF classes to WordPress menu items (used for dynamically changing colors based on ACF field. Utilizes Tailwind CSS utility classes)
/**
* Adds custom classes to WP Menu Items in Primary theme menu (header)
*/
function header_menu_classes( $classes, $item, $args ) {
// Store primary color and saturation from ACF as variable
$primary_color = get_field('primary_color', 'option');
$primary_color_saturation = get_field('primary_color_saturation', 'option');
// If menu-1 aka primary theme menu filter the classes for WP menu-items
if ( 'menu-1' === $args->theme_location ) {
// adds custom classes including dynamic primary color
$classes[] = 'p-4 hover:bg-white hover:text-' . $primary_color . $primary_color_saturation;
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'header_menu_classes', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment