
WP No Category Base patch
By: a guest on
Sep 5th, 2012 | syntax:
PHP | size: 1.45 KB | hits: 64 | expires: Never
add_action( 'init', 'build_taxonomies', 0 );
function build_taxonomies() {
register_taxonomy( 'category', 'post', array(
'hierarchical' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => 'category_name',
'rewrite' => did_action( 'init' ) ? array(
'hierarchical' => false,
'slug' => get_option('category_base') ? get_option('category_base') : 'category',
'with_front' => false) : false,
'public' => true,
'show_ui' => true,
'_builtin' => true,
) );
}
add_filter( 'post_link', 'remove_parent_cats_from_link', 10, 3 );
function remove_parent_cats_from_link( $permalink, $post, $leavename )
{
$cats = get_the_category( $post->ID );
if ( $cats ) {
// Make sure we use the same start cat as the permalink generator
usort( $cats, '_usort_terms_by_ID' ); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent ) {
// If there are parent categories, collect them and replace them in the link
$parentcats = get_category_parents( $parent, false, '/', true );
// str_replace() is not the best solution if you can have duplicates:
// myexamplesite.com/luxemburg/luxemburg/ will be stripped down to myexamplesite.com/
// But if you don't expect that, it should work
$permalink = str_replace( $parentcats, '', $permalink );
}
}
return $permalink;
}