I have a very peculiar requirement, but hopefully I can explain it without being too confusing. I created a page template where I list some properties I get from an external XML file. So far no problems, and let’s say the URL is like this:
http://www.example.com/properties/
Each property has a link that should redirect the user to a “Single Property” page that displays more information about it. I was wondering if there’s a way to make the link like this:
http://www.example.com/properties/123
Where 123
would be the id of the property. So if I have the URL like properties/some_id
I want to be able to load a view file (like the single.php
or page.php
files), but specific to this URL condition.
Is this possible?
Add this to your theme’s functions.php, or put it in a plugin.
This adds a rewrite rule which directs requests to
/properties/
with any combination of numbers following to pagenameproperties
, with the query varproperty_id
set. Just be sure to visit your permalinks settings page and save to flush rewrite rules, so this new rule will be included.In your
page-properties.php
template,get_query_var('property_id')
will return the property id if it was set, if it’s not then show the default properties page.Another way to do it:
Codex Rewrite API/add rewrite rule
Codex Rewrite API/add rewrite tag