getdave / Tanlinell

Boilerplate Wordpress theme for rapid development of new WP themes. Based on the great work of the _s ("Underscore") theme.
GNU General Public License v2.0
6 stars 2 forks source link

Update menu for CPT #309

Closed neilberry001 closed 10 years ago

neilberry001 commented 10 years ago

inc/cpt-current-menu-item.php

<?php
/**
 * Current Menu Item for CPT's
 * Adds the 'current-menu-item' class to menu items when viewing cpt's 
 * allows for cpt to be a descendant of a top level menu item
 */
function current_type_nav_class($classes, $item) {

    global $post;

    $post_type = get_post_type();
    $post_type_obj = get_post_type_object($post_type);

    if ( is_post_type_archive( get_post_type() ) || is_singular( get_post_type() ) ) {

        $archive_post = get_page_by_path( $post_type_obj->has_archive );    

        $ancestors = get_post_ancestors( $archive_post );

        if( in_array( $item->object_id, $ancestors ) ) {

            array_push($classes, 'current-menu-ancestor');
            array_push($classes, 'current_page_ancestor');

        }

        if ( $item->title == $archive_post->post_title ) {

            array_push($classes, 'current-menu-item');
            array_push($classes, 'current_page_item');

        }

    }

    return $classes;
}
add_filter( 'nav_menu_css_class', 'current_type_nav_class', 10, 2 );
?>