fix: music 类型用 ?music=ID 路由 + template_loader
This commit is contained in:
+19
-9
@@ -341,8 +341,8 @@ function kjweji_register_music() {
|
|||||||
'edit_item' => '编辑聆韵',
|
'edit_item' => '编辑聆韵',
|
||||||
),
|
),
|
||||||
'public' => true,
|
'public' => true,
|
||||||
'has_archive' => false,
|
'has_archive' => true,
|
||||||
'rewrite' => array('slug' => 'music/%post_id%'),
|
'rewrite' => array('slug' => 'music'),
|
||||||
'show_in_rest' => true,
|
'show_in_rest' => true,
|
||||||
'supports' => array('title', 'editor', 'author', 'thumbnail'),
|
'supports' => array('title', 'editor', 'author', 'thumbnail'),
|
||||||
'menu_position' => 27,
|
'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_action('init', function() {
|
||||||
add_rewrite_rule('^music/([0-9]+)/([^/]+)/?$', 'index.php?post_type=music&p=$matches[1]', 'top');
|
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;
|
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;
|
||||||
|
});
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ $query = new WP_Query(array(
|
|||||||
<?php if ($query->have_posts()) : ?>
|
<?php if ($query->have_posts()) : ?>
|
||||||
<div class="music-list">
|
<div class="music-list">
|
||||||
<?php while ($query->have_posts()) : $query->the_post(); ?>
|
<?php while ($query->have_posts()) : $query->the_post(); ?>
|
||||||
<a href="<?php the_permalink(); ?>" class="music-item">
|
<a href="<?php echo home_url("/?music=" . get_the_ID()); ?>" class="music-item">
|
||||||
<?php if (has_post_thumbnail()) : ?>
|
<?php if (has_post_thumbnail()) : ?>
|
||||||
<div class="music-item__cover">
|
<div class="music-item__cover">
|
||||||
<?php the_post_thumbnail('thumbnail', array('alt' => get_the_title())); ?>
|
<?php the_post_thumbnail('thumbnail', array('alt' => get_the_title())); ?>
|
||||||
|
|||||||
Reference in New Issue
Block a user