Dynamic subpage with custom template

Hello everyone,

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
add_rewrite_tag("%name%", "([^/]+)");
  add_rewrite_rule(
      "^restaurant/([^/]+)/review/?",
      'index.php?name=$matches[1]&post_type=restaurant&restaurant_reviews=1',
      "top"
  );
  • We had in mind to watch for the query parameter restaurant_reviews and use the active_templates? filter to change the template
global $wp_query, $wp, $wp_rewrite;
    
    if($wp_query->get("restaurant_reviews") == 1) {
        $active_templates["content"] = 62344;
    }

    return $active_templates;

The issue

  • Unfortunately, we get redirected to /restaurant/<name>and the template does not load.

The question

Is what we try to achieve possible with Bricks?
If so, what step are we missing?

We would appreciate any help with this.
Thank you in advance!

1 Like

I noticed in your add_rewrite_rule() you have ‘review’ instead of ‘reviews’ … would like to see if that makes a difference!

Hi @GratuiTous,

good spot.
But this was just a typo, when writing the post.

We still face the same issue unfortunately

@Florian Did you manage to find a solution?

I have a similar problem.

@Florian @james

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.

Rewrite tag and rule (in my functions.php):

function add_author_custom_rewrite_rules() {
    add_rewrite_rule(
        '^users/([^/]+)/followers/?$', 
        'index.php?author_name=$matches[1]&author_page=followers', 
        'top'
    );
    add_rewrite_rule(
        '^users/([^/]+)/following/?$', 
        'index.php?author_name=$matches[1]&author_page=following', 
        'top'
    );
}
add_action('init', 'add_author_custom_rewrite_rules');

function add_author_custom_query_vars($vars) {
    $vars[] = 'author_page';
    return $vars;
}
add_filter('query_vars', 'add_author_custom_query_vars');

function author_custom_template_redirect() {
    $author_page = get_query_var('author_page');
    if ($author_page == 'followers' || $author_page == 'following') {
        include(get_stylesheet_directory() . '/member-followers_following.php');
        exit;
    }
}
add_action('template_redirect', 'author_custom_template_redirect');

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.

Hope that helps!

@mbgood That’s awesome! Thanks :+1: