0

I have defined a custom URL that loads a custom template, as a customized "archive" por a custom post type.

I can't use the native archive since the page has a couple requisites that the archive doesn't fill, and I'm still using the default archive in a different view.

The thing is, when you use the default 'category' entries in the menu, WP is smart enough to apply the class current_menu_parent to the appropriate entry in the menu, and you can style it accordingly.

But in my situation I have a menu entry that links to custom_archive, where you can list see the pertinent CPTs, but when you go into a single view WP doesn't (can't?) know that custom-archive is some kind of parent to it (and then I can't style the menu).

Is there a workaround to choose an arbitrary url as 'parent' to a post-type? Note: this URL is not an actual post object, just a url_rewrite and a template_redirect.

Thanks and regards.

1 Answer 1

0

Ended up hooking the nav_menu_css_class filter, and adding the appropriate class when needed.

Like this:

add_filter( 'nav_menu_css_class', 'add_parent_menu_class', 10, 2 );
function add_parent_menu_class( $classes, $item ) {

    $post = get_post();
    if ( ! $post ) {
        return $classes;
    }

    if ( $item->url === get_bloginfo( 'url' ) . '/custom_url' && get_post_type() == $this->post_type ) {
        $classes[] = 'current-menu-parent';
    }

    return $classes;
}

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.