Ad

Custom Post Type/taxonomy URL Structure

- 1 answer

I've had a quick scan through similar questions, but most don't seem to answer my question really.

I have a custom post type called, Products. Within there, I have taxonomies such as Room Type, Furniture Type, Material etc etc.

The site requires a really specific url structure to adhere to it's SEO strategy.

At the moment, i'm getting something like this:

/room-type/handles/bathroom
/room-type/handles/kitchen
/room-type/handles/modern

I need to remove Room Type, essentially. I have tried setting "with_front" to false when registering the taxonomy, to no success.

Below is my current code, any help hugely appreciated.

register_taxonomy(

    'room-type',
    'products',

    array(

        'label' => __( 'Room Type' ),
        'rewrite' => array( 

            'slug' => 'room-type',
            'with_front' => true,
            'hierarchical' => true,

        ),

        'hierarchical' => true,

    )

);

UPDATE

So I did a little digging and found this page: https://wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule

...and this is the code I have ended up with - which at the moment appears to be the closest thing I have come up with yet:

function sb_rewrites() {

    add_rewrite_rule( 'handles/([^/]+)/?', 'index.php?room-type=$matches[1]', 'top' );

}

add_action( 'init', 'sb_rewrites', 10, 0 );

function strip_room_type( $link, $term, $taxonomy ){

    if ( $taxonomy !== 'room-type' )

        return $link;

    return str_replace( 'room-type/', '', $link );

}

add_filter( 'term_link', 'strip_room_type', 10, 3 );

Thanks!

Ad

Answer

As I understand your question, I'm not sure there's a perfect way to remove that taxonomy slug. WordPress needs to know what sub taxonomy you're asking for in the URL, so it needs some sort of identifier. However, it appears there may be a work around in an answer to a similar question:

https://wordpress.stackexchange.com/questions/21076/remove-taxonomy-base-or-term-from-url

Essentially, defining verbose rewrite rules to 'force' the term out. But this can cause problems, which are better explained in the accepted answer here:

https://wordpress.stackexchange.com/questions/42120/remove-slug-in-taxonomy-url

Ad
source: stackoverflow.com
Ad