I have a custom post type called "locations".
I have a location called "starbucks". I can view this by going to "/location/starbucks".
I would like to add "coffee-shop" to the URL, so the path "/location/coffee-shop/starbucks" will load the "starbucks" page, while maintaining the URL in the browser.
i have added this code to my functions.php
function add_rewrite_rules()
{
add_rewrite_rule('^location/([^/]*)/([^/]*)/?','index.php?pagename=$matches[2]','top');
}
add_action('init', 'add_rewrite_rules');
Which correctly loads the "starbucks" page, but it changes the URL to "/location/starbucks".
How can I load the "starbucks" page, and keep my custom URL?
location=$matches[2]
, notpagename
.locations
instead (note the plural).