1

Is it possible to have a custom taxonomy navigation, with added active/current-menu classes if the post your on is within that category/custom tax?

e.g you have a custom taxonomy called... Weather. Within that you have Sunny,Windy, Raining.

Whenever you appear on a Sunny page, whether its the archive of the custom taxonomy, or a single.php or most importantly a child post of a post in the sunny page, the Sunny button is highlighted?

Is the only way to manually list then categories then apply if in echo active?

I tried added a custom menu and adding via the admin, but it just had the usual menu item, it didn’t indicate have children or anything.

Unsure if this is possible at all.

1 Answer 1

1

I believe the behavior you're hoping for is not part of WordPress core. The last time I needed this, I adapted a couple functions out of the Post Type Archive Link plugin used for improved post type highlighting. To use it, just add the menu items the normal way and put these functions in a plugin or theme.

// Get menus to play nicely with the submenu script
// blissfully borrowed from Post Type Archive Links plugin, thanks @stephenharris, @F J Kaiser, @ryancurban
function mrw_tax_archive_current( $items ) {
    foreach ( $items as $item ) {
        if ( 'taxonomy' !== $item->type )
            continue;

        global $post;

        if( !$post )
            continue;

        $taxonomy = $item->object;
        $taxonomy_term = $item->object_id;
        if (
            ! is_tax( $taxonomy, $taxonomy_term )
            AND ! has_term( $taxonomy_term, $taxonomy, $post->ID )
        )
            continue;

        // Make item current
        $item->current = true;
        $item->classes[] = 'current-menu-item';

        // Loop through ancestors and give them 'parent' or 'ancestor' class
        $active_anc_item_ids = mrw_get_item_ancestors( $item );
        foreach ( $items as $key => $parent_item ) {
            $classes = (array) $parent_item->classes;

            // If menu item is the parent
            if ( $parent_item->db_id == $item->menu_item_parent ) {
                $classes[] = 'current-menu-parent';
                $items[ $key ]->current_item_parent = true;
            }

            // If menu item is an ancestor
            if ( in_array( intval( $parent_item->db_id ), $active_anc_item_ids ) ) {
                $classes[] = 'current-menu-ancestor';
                $items[ $key ]->current_item_ancestor = true;
            }

            $items[ $key ]->classes = array_unique( $classes );
        }
    }

    return $items;
}
add_filter('wp_nav_menu_objects','mrw_tax_archive_current');

function mrw_get_item_ancestors( $item ) {
    $anc_id = absint( $item->db_id );

    $active_anc_item_ids = array();
    while (
        $anc_id = get_post_meta( $anc_id, '_menu_item_menu_item_parent', true )
        AND ! in_array( $anc_id, $active_anc_item_ids )
    )
        $active_anc_item_ids[] = $anc_id;

    return $active_anc_item_ids;
}
1
  • Thank you, i used a combo of this and added a metabox for the client to tick if they wanted it active, not great i know but only thing i could think of.
    – user4630
    Aug 22, 2014 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.