WordPress-Post-Type-Archive-Links icon indicating copy to clipboard operation
WordPress-Post-Type-Archive-Links copied to clipboard

Active state deducted wrongly

Open davidmosterd opened this issue 11 years ago • 3 comments

Hi,

We have a site that has a menu-item that has an post-type archive link. But, a single post of this archive (which has children) is also in the menu.

When I click on the single, the archive assumes it also the current menu item. Something in the deduction wether it's an ancestor or not and wether it's current goes wrong.

One pointer: don't use the current-menu-item class unless it's an archive and not a single. You do this check, but you check on post_type_archive and singular (!) The checks need to be more thorough like:

if ( is_post_type_archive( $post_type) && ! is_single() ) $classes[] = 'current-menu-item'

Not sure how to help out, I have fair bit of understanding on archives & nav. If I have the time, I will submit a pull-request.

For now, may you have a quick fix?

davidmosterd avatar Nov 15 '13 16:11 davidmosterd

If I recall correctly this is by design.

The idea is that a menu doesn't usually contain each post of a given type, and that - in the absence of such links - the menu link you want associated with a particular post (of given type) is the archive link.

That is, the menu link represents not just the archive page for post type X, but also any individual post of type X.

stephenharris avatar Nov 15 '13 16:11 stephenharris

I would agree, but being called the current-menu-item? parent/ancestor seems fine, and there is code in place, but does not work...

Don't get how that is by design? Or am I missing someting?

davidmosterd avatar Nov 15 '13 22:11 davidmosterd

I agree with david that it does not make sense to have it called current-menu-item. I have a menu where one of the custom posts is a link in the menu as well as the archive resulting in two menu items with the class 'current-menu-item'.

Anyone looking for this desired functionality the following code worked for me:

    //FIX FOR POST TYPE ARCHIVE BEING ACTIVE ON SINGLE PAGE
    function fix_maybe_make_current_archive($items){

        foreach ( $items as $key => $item ) {

            if ( 'post_type_archive' !== $item->type )
                continue;

            $post_type = $item->object;
            //if we are not on the archive page and the current-menu-item is on this menu archive menu item
            if( !is_post_type_archive( $post_type ) && false !== $key = array_search('current-menu-item', $item->classes) ){
                $item->classes[ $key ] = 'current-menu-item-archive';
            }

    }
    add_filter( 'wp_nav_menu_objects','fix_maybe_make_current_archive',999, 1);

mikes000 avatar Jul 26 '16 19:07 mikes000