我设置了两种自定义帖子类型:节拍和曲目。我为这两种自定义帖子类型设置了分类“标记”和“样式”。
自定义帖子类型的永久链接在函数中设置。php:beats/%style%/%postname%和tracks/%style%/%postname%
我有一个问题,当url是/beats/%style%和/tracks/%style%而没有postname(归档)时。然后,所有以%样式%显示的“beats”和“tracks”帖子都将可见。不仅是节拍或风格税的曲目。
当我对get\\u post\\u type()进行回显时,回显总是“beats”。即使url为/tracks/%style%。
这是我的代码:
<?php
add_action(\'init\', \'reg_music\');
function reg_music() {
register_taxonomy(
\'style\',
array (
0 => \'beats\',
1 => \'tracks\',
),
array(
\'label\' => \'Styles\',
\'singular_label\' => \'Style\',
\'hierarchical\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'beats\',\'slug\' => \'tracks\'),
)
);
$labels = array(
\'name\' => _x(\'Beats\', \'post type general name\'),
\'singular_name\' => _x(\'Beat\', \'post type singular name\')
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => \'beats\',
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'excerpt\',\'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\',),
\'rewrite\' => array(
\'slug\' => \'beats/%style%\',
\'with_front\' => false
),
\'has_archive\' => true
);
register_post_type( \'beats\' , $args );
$labels = array(
\'name\' => _x(\'Tracks\', \'post type general name\'),
\'singular_name\' => _x(\'Track\', \'post type singular name\')
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => \'tracks\',
\'capability_type\' => \'post\',
\'hierarchical\' => true,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'excerpt\',\'trackbacks\',\'custom-fields\',\'comments\',\'revisions\',\'thumbnail\',\'author\',\'page-attributes\',),
\'rewrite\' => array(
\'slug\' => \'tracks/%style%\',
\'with_front\' => false
),
\'has_archive\' => true
);
register_post_type( \'tracks\' , $args );
// flush_rewrite_rules();
}
add_filter(\'post_type_link\', \'music_permalink_structure\', 10, 4);
function music_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, \'%style%\' ) ) {
$event_type_term = get_the_terms( $post->ID, \'style\' );
$post_link = str_replace( \'%style%\', array_pop( $event_type_term )->slug, $post_link );
}
return $post_link;
}
register_taxonomy(\'tags\',array (
0 => \'beats\',
1 => \'tracks\',
),array( \'hierarchical\' => false, \'label\' => \'Tags\',\'show_ui\' => true,\'query_var\' => true,\'rewrite\' => array(\'slug\' => \'tag\'),\'singular_label\' => \'Tag\') );
?>