这应该是可能的。首先,你很幸运www.mytravelblog.com/jakarta/myPostName/
已经可以使用了,它会向您显示帖子,不会将您重定向到较长的版本(至少在我看来是可行的)。这意味着您只需处理生成的链接,而不必干扰传入URL的处理方式或“规范化”。
结果get_permalink()
, 将通过post_link
. 所以你可以这样做:
add_filter( \'post_link\', \'remove_parent_cats_from_link\', 10, 3 );
function remove_parent_cats_from_link( $permalink, $post, $leavename )
{
$cats = get_the_category( $post->ID );
if ( $cats ) {
// Make sure we use the same start cat as the permalink generator
usort( $cats, \'_usort_terms_by_ID\' ); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent ) {
// If there are parent categories, collect them and replace them in the link
$parentcats = get_category_parents( $parent, false, \'/\', true );
// str_replace() is not the best solution if you can have duplicates:
// myexamplesite.com/luxemburg/luxemburg/ will be stripped down to myexamplesite.com/
// But if you don\'t expect that, it should work
$permalink = str_replace( $parentcats, \'\', $permalink );
}
}
return $permalink;
}