1

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

  • Hi Jascha - Just so I'm clear, you would like a url structure like this... mysite.io/parent-category/sub-category ? Also, is this a regular post category or a custom taxonomy? – Louis S Aug 2 at 23:27
  • Hi Louis, yes that is exactly the URL structure I’d want to have for the archive pages of subcategories. I believe the category is a custom taxonomy set up by the parent theme, not by myself. I’m sorry that I did not think this would be relevant for the solution because I was thinking that URL structure is changed in some global settings.. I will edit my question to make it more precise. Is there some hook that I can use to alter a taxonomy that has already been registered? Thanks for your effort! – Jascha Goltermann Aug 3 at 6:48
2
+100

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 ).

  • Quite indeed, I totally forgot there was the rewrite hierarchial parameter, too. I need to update my own answer to include it also. – Antti Koskinen Aug 3 at 14:03
0

Is there some hook that I can use to alter a taxonomy that has already been registered?

Yes, there is the register_taxonomy_args filter, which you can use to modify a registered taxonomy.

function prefix_register_taxonomy_args_my_taxonomy( $args, $taxonomy, $object_type ) {    
  if ( 'my_taxonomy' !== $taxonomy ) {
    return $args;
  }

  $args['hierarchical'] = true; // have descendants, like categories

  if ( ! is_array( $args['rewrite'] ) ) {
    $args['rewrite'] = array(
      'hierarchical' => true, // allow hierarchical urls, defaults to false
    )
  }  

  return $args;      
}
add_filter( 'register_taxonomy_args', 'prefix_register_taxonomy_args_my_taxonomy', 10, 3 );
  • Hi Jascha - Antti's answer above will allow you to convert this custom taxonomy to hierarchical but in your question you mentioned that the sub cat was already a child of a parent cat so that means this custom taxonomy is already hierarchical? If it is already hierarchical then by default it should already be displaying the archive page url like this mysite.io/parent-category/sub-category – Louis S Aug 3 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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