Skip to content

Instantly share code, notes, and snippets.

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 mattradford/da1331e315061a298ec68ef43c69f550 to your computer and use it in GitHub Desktop.
Save mattradford/da1331e315061a298ec68ef43c69f550 to your computer and use it in GitHub Desktop.
Highlight menu item based on Yoast Primary Category
/**
* Highlight menu item based on Yoast Primary Category
*/
function td_yoast_primary_category_nav_class( $classes, $item ) {
if ( is_single() ) {
global $post;
if ( $primary_category = get_post_meta($post->ID,'_yoast_wpseo_primary_category',true) ) {
$primary_category_term = get_term_by( 'id', $primary_category, 'category' );
$menu_locations = get_nav_menu_locations();
if ( has_term($menu_locations['primary_navigation'], 'nav_menu', $item) ) {
if (in_array('menu-' . $primary_category_term->slug , $classes) ){
$classes[] = 'yoast-primary-category';
}
}
}
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'td_yoast_primary_category_nav_class', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment