Skip to main content
added 360 characters in body
Source Link
JoLoCo
  • 111
  • 1
  • 4

I have a site which has pages that show products (these aren't sold on the site, though, it's just informational).

Some categories have subcategories, so the URI for a subcategory archive page looks like this:

/products/category-1/category-1-child/

But some categories don't have any subcategories, so the URI for a product post in that category looks like this:

/products/category-2/product-name/

The issue I'm having is thatSo either the WPfirst URL rewriterworks and the 2nd one is having trouble telling which URL points to a subcategory404, or the second URL works and which points to a product post.

Asthe first one is 404, as either a subcategory name or a post slug can be in that third position in the URL,.

I can only seemsuspect that the WP's built-in process which resolves permalinks to get eitheran internal URL which has the relevant information in query vars, seems to be unable to tell whether the URL points to a subcategory or thea product post working at those URLsin a single-level category.

I've tried all sorts of settings using rewrite_rules_array but I can only ever get one or the other working.

Here's what I have in the rewrite_rules_array that works for posts but not subcategories, for example:

$new_rules['products/([^/]+)/([^/]+)/?$'] = 'index.php?product_category=$matches[1]&product=$matches[2]';

And here's a rule which works for a subcategory archive page, but fails on a single-level-category post:

$new_rules['products/([^/]+)/([^/]+)/?$'] = 'index.php?product_category=$matches[2]';

(Though this is also the default behaviour if I don't add anything.)

Note that the array key is the same for both URLs, as they both have the same URL format. I'm sure I'm missing something simple here!

I have a site which has pages that show products (these aren't sold on the site, though, it's just informational).

Some categories have subcategories, so the URI for a subcategory archive page looks like this:

/products/category-1/category-1-child/

But some categories don't have any subcategories, so the URI for a product post in that category looks like this:

/products/category-2/product-name/

The issue I'm having is that the WP URL rewriter is having trouble telling which URL points to a subcategory, and which points to a product post.

As either a subcategory name or a post slug can be in that third position in the URL, I can only seem to get either the subcategory or the post working at those URLs.

I've tried all sorts of settings using rewrite_rules_array but I can only ever get one or the other working.

I have a site which has pages that show products (these aren't sold on the site, though, it's just informational).

Some categories have subcategories, so the URI for a subcategory archive page looks like this:

/products/category-1/category-1-child/

But some categories don't have any subcategories, so the URI for a product post in that category looks like this:

/products/category-2/product-name/

So either the first URL works and the 2nd one is 404, or the second URL works and the first one is 404, as either a subcategory name or a post slug can be in that third position in the URL.

I suspect that the WP's built-in process which resolves permalinks to an internal URL which has the relevant information in query vars, seems to be unable to tell whether the URL points to a subcategory or a product post in a single-level category.

I've tried all sorts of settings using rewrite_rules_array but I can only ever get one or the other working.

Here's what I have in the rewrite_rules_array that works for posts but not subcategories, for example:

$new_rules['products/([^/]+)/([^/]+)/?$'] = 'index.php?product_category=$matches[1]&product=$matches[2]';

And here's a rule which works for a subcategory archive page, but fails on a single-level-category post:

$new_rules['products/([^/]+)/([^/]+)/?$'] = 'index.php?product_category=$matches[2]';

(Though this is also the default behaviour if I don't add anything.)

Note that the array key is the same for both URLs, as they both have the same URL format. I'm sure I'm missing something simple here!

Source Link
JoLoCo
  • 111
  • 1
  • 4

How to work with URLs where sometimes a post or a subcategory is in the same part of the URL structure

I have a site which has pages that show products (these aren't sold on the site, though, it's just informational).

Some categories have subcategories, so the URI for a subcategory archive page looks like this:

/products/category-1/category-1-child/

But some categories don't have any subcategories, so the URI for a product post in that category looks like this:

/products/category-2/product-name/

The issue I'm having is that the WP URL rewriter is having trouble telling which URL points to a subcategory, and which points to a product post.

As either a subcategory name or a post slug can be in that third position in the URL, I can only seem to get either the subcategory or the post working at those URLs.

I've tried all sorts of settings using rewrite_rules_array but I can only ever get one or the other working.