Advertisement
mbis

Use custom slugs in permalinks

Mar 18th, 2020
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.70 KB | None | 0 0
  1. function pm_use_custom_parent_slugs($full_custom_slug, $post) {
  2.     global $permalink_manager_uris;
  3.  
  4.     if(!empty($post->ancestors) && $post->ancestors && is_post_type_hierarchical($post->post_type)) {
  5.         $full_slug = $post->post_name;
  6.  
  7.         foreach($post->ancestors as $parent) {
  8.             $parent = get_post($parent);
  9.  
  10.             if($parent && $parent->post_name) {
  11.                 $custom_slug = (!empty($permalink_manager_uris[$parent->ID])) ? basename($permalink_manager_uris[$parent->ID]) : '';
  12.                 $full_slug = (!empty($custom_slug)) ? "{$custom_slug}/$full_slug" : "{$parent->post_name}/{$full_slug}";
  13.             }
  14.         }
  15.     }
  16.  
  17.     return $full_slug;
  18. }
  19. add_filter('permalink_manager_filter_default_post_slug', 'pm_use_custom_parent_slugs', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement