Created
April 27, 2022 10:32
-
-
Save sachithrrra/4ca1ab760cbc568c9c35a5417e1ba1d3 to your computer and use it in GitHub Desktop.
Remove child categories from Wordpress permalink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Remove child categories from Wordpress permalink (https://example.com/%category%/%postname%/ custom structure) | |
https://example.com/category/sub-category/post-name to https://example.com/category/post-name | |
*/ | |
function remove_child_categories_from_permalinks( $category ) { | |
while ( $category->parent ) { | |
$category = get_term( $category->parent, 'category' ); | |
} | |
return $category; | |
} | |
add_filter( 'post_link_category', 'remove_child_categories_from_permalinks' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment