I have a WordPress website with a custom post type 'team-bio' with the single pages showing the bio of the respective person.
website.com/team-bio/person-name
I would like to show a different custom post type single page template so that each person has a page that would show alternate information about this person when the user visits an alternate url
website.com/dbc/person-name
I've tried the following code but the /dbc/ pages go to a 404 page.
// Add custom rewrite rules for the custom post type
function custom_post_type_rewrite_rules() {
// Define the custom post type name
$post_type = 'team-bio';
// Define the custom URL structure for the first template
$structure1 = '/team-bio/%postname%/';
// Define the custom URL structure for the second template
$structure2 = '/dbc/%postname%/';
// Add the first rewrite rule
add_rewrite_rule($structure1, 'index.php?post_type=' . $post_type . '&name=$matches[1]', 'top');
// Add the second rewrite rule
add_rewrite_rule($structure2, 'index.php/dbc/?post_type=' . $post_type . '&name=$matches[1]', 'top');
}
add_action('init', 'custom_post_type_rewrite_rules');
// Add custom templates for the custom post type
function custom_post_type_templates($template) {
// Define the custom post type name
$post_type = 'team-bio';
// Get the post
$post = get_query_var('post_type');
// Check if it's the custom post type and the URL structure matches the first template
if ($post == $post_type && strpos($_SERVER['REQUEST_URI'], '/team-bio/') !== false) {
// Load the custom template for the first URL structure
return get_template_directory() . '/templates/single-team-bio.twig';
}
// Check if it's the custom post type and the URL structure matches the second template
if ($post == $post_type && strpos($_SERVER['REQUEST_URI'], '/dbc/') !== false) {
// Load the custom template for the second URL structure
return get_template_directory() . '/templates/dbc.twig';
}
// Return the default template
return $template;
}
add_filter('single_template', 'custom_post_type_templates');
Any help would be appreciated!
I made this work with the following code:
function custom_rewrite_rule() {
add_rewrite_rule('^dbc/([^/]*)/?', 'index.php?post_type=team-bio&name=$matches[1]', 'top');
}
add_action( 'init', 'custom_rewrite_rule' );
// Add custom templates for the custom post type
function custom_post_type_templates($template) {
// Define the custom post type name
$post_type = 'team-bio';
// Get the post
$post = get_query_var('post_type');
// Check if it's the custom post type and the URL structure matches the first template
if ($post == $post_type && strpos($_SERVER['REQUEST_URI'], '/team-bio/') !== false) {
// Load the custom template for the first URL structure
return get_template_directory() . '/templates/single-team-bio.twig';
}
// Check if it's the custom post type and the URL structure matches the second template
if ($post == $post_type && strpos($_SERVER['REQUEST_URI'], '/dbc/') !== false) {
// Load the custom template for the second URL structure
return get_template_directory() . '/templates/dbc.twig';
}
// Return the default template
return $template;
}
add_filter('single_template', 'custom_post_type_templates');