we are looking to implement a dynamic subpage for posts of a custom post type “restaurant”.
This is our setup at the moment:
Custom Post Type restaurant with the following URL structure /restaurant/<restaurant_name>
This Post type uses an Archive template and a Single Post Template
Our goal:
If a user visits the URL /restaurant/<name>/reviews we would like to load a separate template which only contains a code block to include external reviews
Ideally, we would like to access the dynamic data of the restaurant post
We do not want to create an actual post/page for each restaurant (because there are many many restaurants in the system)
Our current (failed) approach
Register a Custom rewrite rule with add_rewrite_rule, add_rewrite_tag and query_vars
I achieved something similar. In my case, I wanted dynamic author subpages that showed a list of followers and following. I ended up using a single template file for both and just conditionally checking the page the user is on but you could easily have a single template file for each. In the template file I echo the Bricks templates I created using their ID.
Template file (in my bricks-child theme directory):
<?php
/* Template Name: Member Followers & Following */
$author_name = get_query_var('author_name');
$author = get_user_by('slug', $author_name);
if (!$author) {
// If no author found, display a 404 error
status_header(404);
nocache_headers();
include(get_query_template('404'));
exit;
}
$author_page = get_query_var('author_page');
get_header();
if ($author_page == 'followers') :
echo do_shortcode( '[bricks_template id="12345"]' );
elseif ($author_page == 'following') :
echo do_shortcode( '[bricks_template id="54321"]' );
endif;
get_footer();
As always, make sure to flush your permalinks and create your templates in Bricks then add your template ID to the template file in your theme directory.