【发布时间】:2023-02-02 02:26:39
【问题描述】:
我有一个 WordPress 网站,其自定义帖子类型为“team-bio”,单个页面显示相应人员的简历。
website.com/team-bio/person-name
我想显示一个不同的自定义帖子类型单页模板,以便每个人都有一个页面,当用户访问备用网址时,该页面将显示有关此人的备用信息
website.com/dbc/person-name
我尝试了以下代码,但 /dbc/ 页面转到 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');
任何帮助,将不胜感激!
【问题讨论】:
标签: php wordpress custom-wordpress-pages timber