fix: music 类型用 ?music=ID 路由 + template_loader

This commit is contained in:
2026-05-19 00:44:36 +08:00
parent 126c2076a7
commit d21487130b
2 changed files with 20 additions and 10 deletions
+19 -9
View File
@@ -341,8 +341,8 @@ function kjweji_register_music() {
'edit_item' => '编辑聆韵',
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'music/%post_id%'),
'has_archive' => true,
'rewrite' => array('slug' => 'music'),
'show_in_rest' => true,
'supports' => array('title', 'editor', 'author', 'thumbnail'),
'menu_position' => 27,
@@ -350,13 +350,6 @@ function kjweji_register_music() {
));
}
add_filter('post_type_link', function($link, $post) {
if ($post->post_type === 'music' && $post->post_status === 'publish') {
return home_url(user_trailingslashit('/music/' . $post->ID . '/' . $post->post_name));
}
return $link;
}, 11, 2);
add_action('init', function() {
add_rewrite_rule('^music/([0-9]+)/([^/]+)/?$', 'index.php?post_type=music&p=$matches[1]', 'top');
});
@@ -507,3 +500,20 @@ add_filter('wp_insert_post_data', function($data) {
}
return $data;
});
// ── music single 路由 ──
add_filter('query_vars', function($vars) {
$vars[] = 'music';
return $vars;
});
add_action('template_include', function($template) {
$music_id = get_query_var('music');
if ($music_id) {
query_posts(array('post_type' => 'music', 'p' => intval($music_id)));
$music_template = get_template_directory() . '/single-music.php';
if (file_exists($music_template)) return $music_template;
}
return $template;
});