Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

WP No Category Base patch

By: a guest on Sep 5th, 2012  |  syntax: PHP  |  size: 1.45 KB  |  views: 85  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }