I have a custom post type resource
. This custom post type shares the category
taxonomy with posts.
For a given category term archive, example url: /category/reducing-inequality/
, I want to have an additional URL that displays ONLY the resource custom post type for that category. I can do so by using a query string:
category/reducing-inequality/?post_type=resource
What I want to do though is create a rewrite rule that will have this category archive (the above URL) instead rewrite to /resource/category/reducing-inequality
Is this possible? Any guidance is much appreciated, thank you!
This is exactly what add_rewrite_rule()
is for. I'd do something like:
function custom_resource_rewrite_rules() {
add_rewrite_rule('^(\w+)/category/([A-Za-z0-9\-\_]+)/?', 'category/$matches[2]/?post_type=$matches[1]', 'top');
}
add_action('init', 'custom_resource_rewrite_rules');
You could, of course, pass resource
to the rewrite directly...but the above is a more general solution that will work for any custom post type.
Note: You'll likely have to head to the WP-Admin and re-save your permalinks to properly "flush" them, in order for this to work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.