Search code examples
phpwordpresscategoriesslug

Is there a way to create duplicated subcategory slugs on Wordpress?


I've googled around for this like crazy, but can't seem to find a not too outdated answer for this question or something that actually works.

My problem is just like the one this guy describes on this forum post (from 4, holy FOUR years ago!): https://wordpress.org/support/topic/duplicate-subcategory-slug-problem

I'm currently using the latest version, Wordpress 4.1 and what I'm trying to achieve is this:

Let's say I have 2 categories on my website, which are called Boots and Shoes. Inside both of them, I want to make a category named Red. That's how my category structure would look like then:

 - Boots // -> URL: http://example.com/boots
 | ----- Red // -> URL: http://example.com/boots/red (GREAT!)

 - Shoes // -> URL: http://example.com/shoes
 | ----- Red // -> URL: http://example.com/shoes/red-shoes (cmon, wp, nooo !)

When what I'd like to achieve, is this:

 - Boots // -> URL: http://example.com/boots
 | ----- Red // -> URL: http://example.com/boots/red

 - Shoes // -> URL: http://example.com/shoes
 | ----- Red // -> URL: http://example.com/shoes/red

This way would be perfect !!!

In case you're wondering, yes, I know that's just part of wordpress and they don't want that to be changed, I understand why and all that jazz, yeah. But for this particular project, I really need this.

I guess I'll probably have to alter the core, not being able to update Wordpress if I do so, I know, but I have no problem with that if I need to.

But if there's another way to make up this hierarchy that doesn't involve categories and doesn't break my URIs, I can try that too. Any help or tip is appreciated guys, thank you very much !


Solution

  • If your subcategory structure doesn't go deeper than shoes/red (not shoes/red/hiking for instance), you can use permalink endpoints to simulate categories. Permalink endpoints are rewritten from domain.com/request/url/?endpoint.

    After each color or similar is set as an endpoint to EP_CATEGORY, category URLs can be suffixed with /red, /blue and so on. Applying the endpoints to EP_ALL_ARCHIVES allows the endpoints on CPT and date archives too.

    Then in a filter or action such as template_include you can read the appended endpoint using the global $wp_query object and filter the category items accordingly.

    If a user enters another suffix to the URL (e.g. /red/hiking), $wp_query will recognize them as a key/value pair as red => hiking (?red=hiking as a raw query string) in the query variables ($wp_query -> get( 'red' ); returns hiking). This might be a good or a bad thing depending on how you implement the logic.

    After you add permalink endpoints, you need to flush your installation's rewrite rules either using the function flush_rewrite_rules() or by going to the admin panel permalinks management section and pressing update once.

    Note: You can't use dynamic names for endpoints. You need to define a static endpoint for each variant (red, blue, etc.).