Advertisement
Guest User

WP No Category Base patch

a guest
Sep 5th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. add_action( 'init', 'build_taxonomies', 0 );  
  2.  
  3. function build_taxonomies() {
  4.   register_taxonomy( 'category', 'post', array(
  5.         'hierarchical' => true,
  6.         'update_count_callback' => '_update_post_term_count',
  7.         'query_var' => 'category_name',
  8.         'rewrite' => did_action( 'init' ) ? array(
  9.                     'hierarchical' => false,
  10.                     'slug' => get_option('category_base') ? get_option('category_base') : 'category',
  11.                     'with_front' => false) : false,
  12.         'public' => true,
  13.         'show_ui' => true,
  14.         '_builtin' => true,
  15.     ) );
  16. }
  17.  
  18. add_filter( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
  19. function remove_parent_cats_from_link( $permalink, $post, $leavename )
  20. {
  21.     $cats = get_the_category( $post->ID );
  22.     if ( $cats ) {
  23.         // Make sure we use the same start cat as the permalink generator
  24.         usort( $cats, '_usort_terms_by_ID' ); // order by ID
  25.         $category = $cats[0]->slug;
  26.         if ( $parent = $cats[0]->parent ) {
  27.             // If there are parent categories, collect them and replace them in the link
  28.             $parentcats = get_category_parents( $parent, false, '/', true );
  29.             // str_replace() is not the best solution if you can have duplicates:
  30.             // myexamplesite.com/luxemburg/luxemburg/ will be stripped down to myexamplesite.com/
  31.             // But if you don't expect that, it should work
  32.             $permalink = str_replace( $parentcats, '', $permalink );
  33.         }
  34.     }
  35.     return $permalink;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement