admin管理员组

文章数量:1122846

I have a custom post type "treatment", under this CPT the taxonomy "treatment_category" which forntend slug is "category"

right now the URL for category displaying as follows.

www.MYDOMAIN.COM/category/practitioner-london/

Just want to remove the slug "category" and display as

www.MYDOMAIN.COM/practitioner-london/

also the post URLs like this

www.MYDOMAIN.COM/treatment/practitioner-london/dermal-filler-london/

also want to change this to as below by removing "treatment"

www.MYDOMAIN.COM/practitioner-london/dermal-filler-london/

is this possible.

I have a custom post type "treatment", under this CPT the taxonomy "treatment_category" which forntend slug is "category"

right now the URL for category displaying as follows.

www.MYDOMAIN.COM/category/practitioner-london/

Just want to remove the slug "category" and display as

www.MYDOMAIN.COM/practitioner-london/

also the post URLs like this

www.MYDOMAIN.COM/treatment/practitioner-london/dermal-filler-london/

also want to change this to as below by removing "treatment"

www.MYDOMAIN.COM/practitioner-london/dermal-filler-london/

is this possible.

Share Improve this question asked Mar 31, 2024 at 6:10 user1419170user1419170 1012 bronze badges 2
  • You can try plugin wordpress.org/plugins/remove-category-url. For me, I still prefer to keep the category – Công Tử Huyết Commented Mar 31, 2024 at 15:24
  • Thanks, I tried but not the solution I want. – user1419170 Commented Apr 1, 2024 at 9:25
Add a comment  | 

1 Answer 1

Reset to default 0

yes, possible to achieve the URL structure you desire by modifying the permalink structure for your custom post type and taxonomy.

Firslty flush the rewrite rules by visiting the permalink settings page in your Wp admin area Settings > Permalinks and clicking Save Changes. This will regenerate the rewrite rules and apply your custom permalink structure.

After adding the below code in your current active theme functions.php file or your custom plugin.

Remove treatment_category from the category URL

add_action('init', 'custom_taxonomy_rewrite_rule');
function custom_taxonomy_rewrite_rule() {
    $taxonomy_slug = 'treatment_category'; // Change this to your taxonomy slug
    $taxonomy = get_taxonomy($taxonomy_slug);
    $taxonomy->rewrite['slug'] = ''; // Set the slug to empty to remove "category" from the URL
}

Remove treatment from the post URL

add_action('init', 'custom_post_type_rewrite_rule');
function custom_post_type_rewrite_rule() {
    $post_type_slug = 'treatment'; // Change this to your custom post type slug
    $post_type = get_post_type_object($post_type_slug);
    $post_type->rewrite['slug'] = ''; // Set the slug to empty to remove "treatment" from the URL
}

本文标签: phpRemove slug and custom post type name from URL