WordPress archive permalink with leaf category

You can filter term_link to modify any taxonomy URL generated by the API. In this case, we only need the filter, as the default rewrite rules will handle this modification for the incoming requests. If you’re doing any sort of change that introduces a new URL pattern, you will also need to add new rules or change the existing ones so these new URLs can successfully parse into the correct queries and not just 404.

If you look at the get_term_link API function source, you will see the branch where it checks if it’s a hierarchical taxonomy. I just copy/pasted some bits from the non-hierarchical case so the output is the same as a non-hierarchical term.

add_filter( 'term_link', 'wpd_term_link_filter', 10, 3 );
function wpd_term_link_filter( $url, $term, $taxonomy ) {
    if( 'category' == $taxonomy ){
        global $wp_rewrite;
        $termlink = $wp_rewrite->get_extra_permastruct( $taxonomy );
        $termlink = str_replace( "%$taxonomy%", $term->slug, $termlink );
        $url = home_url( user_trailingslashit($termlink, 'category') );
    }
    return $url;
}