我创建自定义帖子类型:
$rewrite = array(
\'slug\' => \'%categories%\',
\'with_front\' => false,
\'pages\' => true,
\'feeds\' => true,
);
$args = array(
// ...Other options
\'taxonomies\' => array( \'categories\' ),
\'hierarchical\' => false,
\'public\' => true,
\'has_archive\' => \'applications\',
\'publicly_queryable\' => true,
\'rewrite\' => $rewrite,
// Other options...
);
register_post_type( \'applications\', $args );
分类法:
$args = array(
// ...Other options
\'hierarchical\' => true,
\'public\' => true,
// Other options...
);
register_taxonomy( \'categories\', array( \'applications\' ), $args );
此函数用于将层次分类slug添加到post url:
function remove_cpt_slug( $post_link, $post ) {
if ( \'applications\' != $post->post_type || \'publish\' != $post->post_status || strpos($post_link, \'%categories%\') === FALSE) {
return $post_link;
}
$terms = wp_get_object_terms( get_the_ID(), \'categories\', array( \'fields\' => \'slugs\' ) );
if ( !is_wp_error($terms) && !empty($terms) ){
$taxurl = implode(\'/\', $terms );
} else {
return str_replace( \'%categories%/\', \'\', $post_link );
}
return str_replace( \'%categories%\', $taxurl , $post_link );
}
add_filter( \'post_type_link\', \'remove_cpt_slug\', 10, 2 );
add_filter( \'post_link\', \'remove_cpt_slug\', 10, 2 );
正如我所料,现在为帖子生成了永久链接。但它们重定向到postname结构,并得到404未找到错误。
domain.com/game/puzzle/desert-storm/ will redirect to domain.com/desert-storm/
(任何更改后,我保存永久链接设置-刷新重写)
如何防止此重定向?解决404问题?