I’ve made a PHP template file called page-house.php
(this is standard WordPress template). So the website link is: www.example.com/house
.
I’ve used this page template at /house
and I would like it to be a dynamic url, so I can use /house/1212
to show as a clean URL where its rewritten from /house?houseid=1212
.
This is what we’re trying. We edited the functions.php
file and added in:
function custom_rewrite_tag() {
add_rewrite_tag('%house%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
function custom_rewrite_rule() {
add_rewrite_rule('/house/([^/]*)/?','/house?houseid=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
If I visit www.example.com/house/1212
, I will get a 404 page: Not found error
. When I manually enter www.example.com/house?houseid=1212
, the required template file will load. I’ve gone to settings and refreshed the permalink settings multiple times, but it still doesn’t work.
I think its some regex issue what we cannot figure out. Is there anything else that we need to do?