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

  • Hey can you post you answer here @danbrown – Pullata Praveen Aug 10 '17 at 3:33
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.

  • 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 '14 at 11:58
  • WP offers many Permalink option see the full doc here. Wordpress Codex – Preetom Banerjee Mar 12 '14 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 '14 at 23:01
  • @danbrown How did you go with this? Would love to see your solution posted up =] – Tim Malone Jun 30 '16 at 0:20

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.