Q1 – It seems reasonable, if it checks all of your boxes and gives you a maintenance workflow that works for you.
Q2 – There are at least a few ways you could handle this, I think using rewrite endpoints is a fairly simple and workable solution.
We’ll start with just a single endpoint- for example, say the URL for Best Western is at:
After we add a hotels
endpoint, we’ll also have the URL:
The code to do that:
function wpd_add_my_endpoints(){
add_rewrite_endpoint( 'hotels', EP_PERMALINK );
}
add_action( 'init', 'wpd_add_my_endpoints' );
You can add that to your theme’s functions.php
, or better, put it in your own plugin. You’ll also have to flush rewrite rules whenever you add or remove an endpoint, or otherwise change it. A quick way to do this during testing is to visit the Settings > Permalinks page on the admin side.
Ok, so now you can visit that new URL, but it currently shows the same thing as the original.
To change that, we can check for the presence of our new hotels
query var to detect if that URL is being viewed, and load a different template in that case. We do this via the single_template
filter, which lets us change the template WordPress has chosen for this type of query:
function wpd_endpoint_templates( $template ){
global $wp_query;
if( isset( $wp_query->query_vars['hotels'] ) ){
$template = locate_template( 'single-directory-hotels.php' );
}
return $template;
}
add_filter( 'single_template', 'wpd_endpoint_templates' );
Now you can create a single-directory-hotels.php
file and customize it for your needs. You’ll also now have to modify your related directory_category
template file to link to these new hotels/
URLs.
bonus you also have the ability to add (and read) any arbitrary value after the endpoint, for example:
get_query_var( 'hotels' )
will return whatever that contains.
In Conclusion
That’s cool and all, you say, but those URLs would be better if they were like
That’s possible! It’s just much more complicated than this.