Two things need to be done. First, make your posts URL is different using the post_link
filter.
function _20170117_post_link( $url, $post, $leavename ) {
if( in_category( 'news', $post) ) {
$url = get_site_url() . '/news/' . $post->post_name ;
}
return $url;
}
add_filter( 'post_link', '_20170117_post_link', 10, 3 );
Second, make the rewrite rule for this:
function _20170117_rewrite() {
add_rewrite_rule('^news/(.?.+?)(?:/([0-9]+))?/?$', 'index.php?pagename=$matches[1]
&page=$matches[2]', 'top');
add_rewrite_rule('^news/([^/]+)(?:/([0-9]+))?/?$', 'index.php?name=$matches[1]
&page=$matches[2]', 'top');
flush_rewrite_rules();
}
add_action('init', '_20170117_rewrite');
Note that flush_rewrite_rules();
you don’t need to execute every time. Just once. Or you may goto options-permalink.php
and click Save.
I tested with the following settings:
Related Posts:
- Filtering categories in the permalink structure
- Multiple post categories – single permalink
- Categories’ hierarchy in URL
- Changing the category permalink structure
- Exclude subcategories from the url
- How to display only the parent category in the permalink
- Plural Category Base Slug On Category Archive Page
- How to set up Author archives with sub category URL
- force category base – archive pages work WITH & WITHOUT category base slug in url?
- Add “page-a” slug to category link if user visit category link from “page-a”
- One Specific Category To Permalink
- Read GET parameters in APACHE Mod Rewrite rules for WordPress categories in Posts
- Multiple URL’s based on category for one post
- Remove subcategory and category with some rules from permalink
- WordPress URL Rewrite for dynamic and customized URL
- Category URL Management
- Using custom structure prefix showing 404 for sub-categories
- A different permalink structure for different categories
- How to set the same base url for two different taxonomies?
- Remove “category” from permalink with add_rewrite_rule
- WordPress Category url redirects to subdomain
- Create monthly archive for parent category
- hierarchical taxonomy in permalinks? using same slug
- Remove “?category=” in the URL wordpress ajax filter work for category
- Custom permalink structure leads to be 404 on pagination
- How to get permalinks with category base working with sub-categories
- Allow duplicate permalink slugs for posts in different categories
- Preventing index.php?category_name=something from redirecting
- how to use a different domain/subdomain for authors/catagories on single site?
- How to exclude uncategorized from permalink structure /%category%/%postname%/
- How to get the url to tag & category base set by the user?
- 404 error on subcategory pages
- Rewrite category slug
- How to Include the Parent and Child Category in the Permalink if the Post is Added to Both
- Best and Cleanest way to remove /category/ from category permalinks? [duplicate]
- Root level category, tag, author and archive pages to work
- Combine Multiple Categories Into One URL Slug
- Archive pagination causing 404 with permalinks structure
- WooCommerce change category url and product base
- Duplicate Category and Page URL loads Category instead of the Page
- How to show in search results posts with a particular tag in a particular category?
- Allow dot in wordpress permalinks (only for categories)
- wordpress wp_list_categories
- How to get category URL with the slug?
- How get permalink for the current category or tag?
- Changing permalink from /postname/ to category/postname/
- How to disable default category linkbuilding?
- Show parent category in URL of subcategory archive page
- Mod_Rewrite to show only last subcategory in URL
- Sub Categories Keep Redirecting to Pages With Same Slugs
- How to rewrite the post URL according to the category?
- Removing subcategories from permalinks [duplicate]
- Flat category URLs but retaining hierarchy?
- A single category with a specific permalink structure differing from the standard set for the rest of the site
- How can you change the permalink for pages?
- Remove the parent name from permalink
- Redirect single article permalink to paginated category page
- Hide the word ‘category’ from my permalinks
- WordPress alphabetical A-Z custom post type post result display
- Hierarchical permalink structure for posts showing categories and sub-categories
- Use the category name instead of category slug in permalinks
- Function to automatically rename the base category permalink
- How to remove one specific category (uncategorized) from post permalinks
- Custom Permalink with child categories creating 404
- How to link a PDF file to a category URL
- Redirect year month day postname permalink structure to category postname structure
- category link not working for a specific slug, works for others
- url for posts for a date within a category
- Pagination breaks on child-categories, works fine on parent-category
- Is the part “category” fixed to an URL to an archive or post?
- Custom url structure – pages under a custom post type
- prefix to post permalinks without affecting category permas
- Need help with major category and permalink re-organization
- How to use category slug with a regular page
- How to add tags under categories
- How to remove category name for only one category?
- Subcategories randomly not working after page/post/category edit in wordpress. Fix: saving permalinks twice
- Get category base permalink
- How to remove parent category from child category url using wordpress rewrite rule?
- Category URL’s 404 after setting category base to ‘.’
- Wrong category in URLs accepted like the correct one
- Change Category Base For Custom Post Type or Posts Page
- When category name and title are the same … WP will select archive.php instead of single.php
- Accessible Subcategory URL’s pages without using .php hack
- Hide category name using mod_rewrite
- same permalink for parent category and child category
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- /blog/ permalink prefix causes category pages to 404
- Change category base based on category
- Modify / Rewrite single template url structure
- Most efficient use of custom taxonomies, categories, and pages
- Removing short versions of category archive URL
- Change next_post and previous_link to navigate throught parent categories only
- What approach should I take for this URL structure?
- How to include first assigned category in the post URL?
- how wordpress generates permalinks as /%category%/%post%/
- Multiple Parent Category URLs
- Category archive page has two URLs even when default theme is active with no active plugins
- Use same wordpress category base and tags as the prefix as the post permalink
- WordPress how to remove parent slug from category URLs