Reputation: 13
I would like to remove the category base from Wordpress URL only for specific category. For example, i need to change: mysite.com/category/blog to mysite.com/blog
but i want to keep other categories unchanged: mysite.com/category/songs
I think that it could be achieved with some .htaccess rule, but I found some generic rules that remove the basic category in all the url.
Upvotes: 0
Views: 1389
Reputation: 87
This can be accomplished with some custom filters & actions. Try placing this code in your theme's functions.php file:
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the categories for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "News" ) {
$permalink = trailingslashit( home_url('/'. $post->post_name .'-'. $post->ID .'/' ) );
}
return $permalink;
}
add_action('generate_rewrite_rules', 'custom_rewrite_rules');
function custom_rewrite_rules( $wp_rewrite ) {
// This rule will will match the post id in %postname%-%post_id% struture
$new_rules['^([^/]*)-([0-9]+)/?'] = 'index.php?p=$matches[2]';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite;
}
This will set up the permalink structure that you want for posts:
Upvotes: 2
Reputation: 87
You can easily do that without using plugins. Through Admin panel go to settings->permalinks and select default to custom
here you know more.. https://codex.wordpress.org/Using_Permalinks
Upvotes: 0
Reputation: 1177
you can easily achieve this by using Enhanced Custom Permalinks Wp plugin. you just need to go edit the category, yo will see a field to add your custom url.
https://wordpress.org/plugins/enhanced-custom-permalinks/
Upvotes: 2