2

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

2
  • 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, 2019 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! Aug 3, 2019 at 6:48

3 Answers 3

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

2
  • Quite indeed, I totally forgot there was the rewrite hierarchial parameter, too. I need to update my own answer to include it also. Aug 3, 2019 at 14:03
  • If you are using default categories, and do not want "/category" (removed by adding a dot to Category Base setting) - but just /your-top-category/your-subcategory, this does not seem to work ... archive throws 404.
    – trainoasis
    Sep 1, 2020 at 6:53
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 );
1
  • 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, 2019 at 9:43
0

I was having the same problem,

mysite.io/category/sub-category/post-name is what I have for post

mysite.io/sub-category is what I want to change

mysite.io/category/sub-category is what I want

I bet you have put a . at the settings > permalink optional section, remove it and leave it blank and save. Now install the plugin No Category Base (WPML) or download it.

And now you can check your link.

Hope everything works fine there.

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.

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