Show Parent Category Only in Permalink | WordPress

You want to show only parent category name on slug/url/permalink. So simple add category on permalink structure and add given code in your active theme’s functions.php.

Watch Video

How To Do

First of all set or add category in permalink, So follow simple steps

1212121212
Show Parent Category Only
  • Go to WordPress DASHBOARD.
  • Click on Setting then click to Permalinks.
  • Then Add category and SAVE.

After complete this process, Then open CPANEL and go to active theme’s functions.php

  • Open CPANEL.
  • Then click on pubilc_html.
  • Then click on wp-content then go to theme.
  • Now open active theme’s folder then open or edit functions.php.
function thekroyaard_hide_child_cat_from_url( $category ) {
while ( $category->parent ) {
$category = get_term( $category->parent, 'category' );
}

return $category;
}
add_filter( 'post_link_category', 'thekroyaard_hide_child_cat_from_url' );
  • Paste this code and SAVE functions.php.

Hope this code will work fine. Have another doubt so contact me via contact us form.