WordPressのパーマリンク設定で/%category%/%postname%.htmlのようにしていて子カテゴリを設定している場合、/%category%/部分も階層表示されます。
場合によっては長すぎるということもあるので、そんなときはfunctions.phpに以下のコードを追加することで、子カテゴリーが含まれていても親カテゴリーのみのURLとすることができます。
//子カテゴリを非表示にするフィルタフック
add_filter( 'post_link', 'remove_children_category_slug', 10, 3 );
function remove_children_category_slug( $permalink, $post, $leavename ){
$cats = get_the_category( $post->ID );
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' );
foreach( $cats as $cat ) {
if ( $cat->parent ) {
$parentcategory = explode(" ",get_category_parents( $cat, false, ' ', true ));
$parentcat = $parentcategory[0];
} else {
$parentcat = $cat->slug;
}
}
}
$permalink = home_url()."/".$parentcat."/".$post->post_name;
return $permalink;
}


