Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Last active December 14, 2015 21:50
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 glueckpress/3141551 to your computer and use it in GitHub Desktop.
Save glueckpress/3141551 to your computer and use it in GitHub Desktop.
[WordPress] Add current ancestor class to nav menu when viewing a singular attachment page, e.g. via the image.php template file.
<?php
/**
* Add current item classes for attachment views
*/
function _s_current_attachment_nav_class( $classes, $menu_item ) {
global $wp_query, $post;
if( true == $wp_query->is_singular && 'attachment' == $post->post_type && in_array( $menu_item->object_id, $post->ancestors ) ) {
$classes[] = 'current-menu-ancestor';
}
return $classes;
}
add_filter( 'nav_menu_css_class', '_s_current_attachment_nav_class', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment