[英]Using Secondary Template and URL Structure for Single Custom Post Type Pages
I have a WordPress website with a custom post type 'team-bio' with the single pages showing the bio of the respective person.我有一个 WordPress 网站,带有自定义帖子类型“team-bio”,单页显示相应人员的简历。
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我想显示一个不同的自定义帖子类型单页模板,以便每个人都有一个页面,当用户访问备用 url 时,该页面将显示有关此人的备用信息
website.com/dbc/person-name
I've tried the following code but the /dbc/ pages go to a 404 page.我尝试了以下代码,但 /dbc/ 页面 go 到 404 页面。
// 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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.