
How to Change Permalink Structure for Custom Post Type/Taxonomy
- March 15, 2016
- Leave a comment
Permalinks on a site are crucial, specially for Search Engine Optimization. Let’s say you have a custom post type named as directory and its default URL structure is www.mysite.com/directory/directory-name. But you want to change it by adding the category base of custom post type. So, after the change it should be something like www.mysite.com/directory/my-category/directory-name.
In order to change the permalink structure, first and foremost thing is to change the value of ‘rewrite’ parameter where your custom post type is being registered. It should be like:
1 |
‘rewrite’ => array(‘slug' => ‘directory/%directory-category%'); |
Here ‘directory-category’ is the slug of category/taxonomy of the custom post type named ‘directory’.
After implementing the change described above, you need to add the following code in your Theme’s/Child Theme’s functions.php file:
1 2 3 4 5 6 7 8 9 10 11 |
function pt_wpa_course_post_link( $post_link, $id = 0 ){ $post =get_post($id); if ( is_object( $post ) ){ $terms = wp_get_object_terms( $post->ID, ‘directory-category’); if( $terms ){ return str_replace( '%directory-category%' , $terms[0]->slug , $post_link ); } } return $post_link; } add_filter( 'post_type_link', 'pt_wpa_course_post_link', 1, 3 ); |
This code will lead to the change required in permalink structure of your custom post type including the category/taxonomy in it.
User Comments