I have my permalink structure for posts set up like this: /%category%/%postname%/
This generates URLs like this: mysite.io/category/sub-category/post-name
Thats all fine.
However, the archive page URLs for subcategories are like this: mysite.io/sub-category
Instead, the URLs for subcategories should be like this: mysite.io/category/sub-category
This category type is actually a custom taxonomy registered by the parent theme. I don’t think it is possible to use the permalink settings in the admin dashboard to change the URL structure of this but perhaps there is something I could add to the functions.php to alter the registered taxonomy?
I have found this page in WordPress codex and think it might be useful for finding a solution but my WordPress knowledge seems way too limited to understand how:
‘hierarchical’ – true or false allow hierarchical urls (implemented
in Version 3.1) – defaults to false
Function Reference/register taxonomy
Thanks in advance
You answered your own question yourself.
You want links to custom categories to look like this
{taxonomy_slug}/{parent_term}/{child_term}/{grandchild_term}/
so you should pay attention to two parameters in the register_taxonomy()
arguments: hierarchical
and rewrite
.
$args = [
'hierarchical' => true, // <-- term may have a parent
'labels' => $labels,
'rewrite' => [
// hierarchical urls, defaults to FALSE
'hierarchical' => true, // <--
]
];
Your custom taxonomy is created by the parent theme, so to change it, use the register_taxonomy_args
filter:
add_filter( 'register_taxonomy_args', 'se344007_mytax_args', 10, 2 );
function se344007_mytax_args( $args, $taxonomy )
{
if ( 'mytax' !== $taxonomy ) {
return $args;
}
// it looks like it's already set up by parent theme
// $args['hierarchical'] = true;
if ( !is_array($args['rewrite']) )
$args['rewrite'] = [];
$args['rewrite']['hierarchical'] = true;
return $args;
}
When you register custom taxonomy, the default link to term (custom category)
is {taxonomy_slug}/{child_term_slug}
even if taxonomy is hierarchical, because
by default, the created links are not hierarchical ( $args[‘rewrite’][‘hierarchical’] = false ).