0

In Permalink Settings for Custom Structure we have set it to the following so our News pages have news in the URL:

/news/%postname%/

We have also created custom post types and custom taxonomy for those post types, for example, a directory CPT with a Locations tag.

We have changed the custom post type URL to /directory/ thats fine, but I cannot find how to change the TAG URL structure. Tags only work with the following URLs:

/news/locations/new-york/

But I would want to have it as:

/directory/locations/new-york/

I would also like to do it for categories too.

I cant find anything online.

4
  • So your CPT also supports the post_tag taxonomy and you want another permalink structure with directory being the base, e.g. example.com/directory/tag/foo-tag ? That would lead to duplicate content penalty because both URLs would display the same posts? Or did you want directory/tag/foo-tag to show only posts in your CPT that are tagged with foo-tag?
    – Sally CJ
    Aug 23, 2021 at 11:13
  • But if that is the case, then why don't you just use a custom taxonomy for your CPT, e.g. directory_tag? That way, you'd be able to set the permalink structure to directory/tag/foo-tag all without the extra work of registering custom rewrite rules and filtering the tag link so that it uses the directory base.
    – Sally CJ
    Aug 23, 2021 at 11:13
  • 1
    I am using a custom taxonomy. For example, Locations. So the links are /news/locations/new-york/. I dont want /news/ in it, I want /directories/. I also need /directories/ because I only want to show posts from this taxonomy. Have updated my post to show the locations in the URL perhaps it explains it better. Aug 23, 2021 at 16:30
  • Thanks for the clarification, and in that case, then you'd want to disable the front base in the permalink - see my answer for more details. (I used directory/location, but just change it to whatever you want)
    – Sally CJ
    Aug 24, 2021 at 1:39

1 Answer 1

1

That /news/ part is called front base and it could also be /foo/bar/, i.e. having multiple "folders", and if you don't want the front base to be prepended to the permalink of posts in your post type or terms in your custom taxonomy, then set with_front in the rewrite argument to false, like so:

// Register the "directory" post type.
register_post_type( 'directory', array(
    'label'   => 'Directories',
    'public'  => true,
    'rewrite' => array(
        'slug'       => 'directory',
        // If the front base is /news/ and this is true (which is the default value),
        // then the post permalink would be example.com/news/directory/<post slug>.
        // So set this to false if you don't want the /news/ in the permalink.
        'with_front' => false,
    ),
) );

// Register the "location" taxonomy.
register_taxonomy( 'location', 'directory', array(
    'label'   => 'Locations',
    'public'  => true,
    'rewrite' => array(
        'slug'       => 'directory/location',
        // If the front base is /news/ and this is true (which is the default value),
        // then the term permalink would be example.com/news/directory/location/<term slug>.
        // So set this to false if you don't want the /news/ in the permalink.
        'with_front' => false,
    ),
) );

And then be sure to flush the rewrite rules by simply visiting the Permalink Settings page. (No need to click on the Save Changes button, unless of course if you changed something in the form on the page)

3
  • 1
    Ahh, its the 'rewrite' parameter for register_taxonomy! Not sure how I missed that. Thanks Aug 24, 2021 at 8:50
  • So, would you please accept my answer? (It's nice to see the "green icon" next to questions with an accepted answer..) Or do you want to post an answer?.. :)
    – Sally CJ
    Aug 24, 2021 at 12:03
  • 1
    Done! Sorry I forgot about that, I did vote it up though :) Aug 24, 2021 at 17:44

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.