0

I have a custom post type 'profile'. Each profile can be one and only one of either student, staff or alumni, which is set through the 'profile_types' custom taxonomy. This gives me permalinks of:

www.mysite.com/profile/arthur-mcmillan. 

What I want is:

www.mysite.com/student/arthur-mcmillan

I have actually got it to work using the following technique, but it breaks all my other pages and posts, returning a page not found.

In register profile custom post type:

'rewrite' => array('slug' => '%profile_types%', 'with_front' => false)

And I'm filtering it with this function:

function filter_post_type_link($link, $post)
{      
    if ($post->post_type != 'profile'){
        return $link; 
    }

    if ($cats = get_the_terms($post->ID, 'profile_types')){
        $link = str_replace('%profile_types%', array_pop($cats)->slug, $link); 
    }

    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

So that works, but breaks other ordinary pages and posts. Does anyone know a way to fix the posts and pages, or of a better way of changing the permalink?

Thanks

1
  • Hey can you post you answer here @danbrown
    – Praveen
    Aug 10, 2017 at 3:33

1 Answer 1

1

Use Category base and Tag base Permalink
The Category base and Tag base are prefixes used in URLs for category and tag archives, which look like this:

example.net/wp/category_base/category_name
 example.net/wp/tag_base/tag_name

The default values for these are category and tag. You can change them, but you can't remove them from the URLs altogether.

Custom permalinks work on most systems without any problems, but there are still some conditions where problems occur.

Using %category% with multiple categories on a post When you assign multiple categories to a post, only one can show up in the permalink. The categories are ordered alphabetically. In each group of sub-categories the order will also be alphabetical. (see Manage Categories). The post will still be accessible through all the categories as normal

Example for doing www.mysite.com/student/arthur-mcmillan
1. go to setting and change Permalink to custom structure.
2. Use /%category%/%postname%/
3. Add a category name as student.
You can easily change category name and update your category just goto post>categories pick previous category edit it name and update it.

That'a all.

4
  • Thanks man :) Does this work for a custom taxonomy? and its only the custom post type I want to change the permalinks to, would this change the permalinks for posts and pages as well?
    – danbrown
    Mar 11, 2014 at 11:58
  • WP offers many Permalink option see the full doc here. Wordpress Codex Mar 12, 2014 at 17:29
  • Yeah thanks, there's nothing there that quite answers my question, but I have managed to solve it in a workaround, I'll post it up here when I get chance
    – danbrown
    Mar 13, 2014 at 23:01
  • @danbrown How did you go with this? Would love to see your solution posted up =]
    – Tim Malone
    Jun 30, 2016 at 0:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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