Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I got this code that rewrite the url's for custom posttypes. It works but only for a depth where the posttitle is on the 5th segment. How can i adjust it so it accepts multiple depths?

e.g.:

http://www.mydomain.com/post_type/category1/postname/
http://www.mydomain.com/post_type/category1/child1/postname/
http://www.mydomain.com/post_type/category1/child1/child2/postname/
http://www.mydomain.com/post_type/category1/child1/child2/child3/postname/

i tried adding:

$newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]';

But that did not work because it gave a 404 error while browsing the child3 depth in the example structure above.

Current code:

function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]';
// The above structure only works if the postname is the 5th uri segment
// e.g. http://www.mydomain.com/post_type/category1/child1/child2/child3/postname/
$newRules['product/(.+)/?$']                = 'index.php?pcategory=$matches[1]'; 

return array_merge($newRules, $rules);
}

[UPDATE1]

It also won't work if i change the code to if's and elseif's. With elseif only the top rule is added and there fore i can only browse products where the posttitle is the 5th segment in the url. With if all rules are added but i cannot browse the categories with a depth of 2,3 and 4 but u can browse products in depths where the url is in the 3,4 and 5th segment

function mmp_rewrite_rules($rules) {
$newRules  = array();
if ('index.php?products=$matches[4]'){
$newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]';
} 
elseif ('index.php?products=$matches[3]'){
$newRules['product/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]';
}
elseif ('index.php?products=$matches[2]'){
$newRules['product/(.+)/(.+)/?$'] = 'index.php?products=$matches[2]';
}
// The above structure only works of the postname is the 5th uri segment
$newRules['product/(.+)/?$']                = 'index.php?pcategory=$matches[1]'; 

return array_merge($newRules, $rules);
}
share|improve this question

2 Answers 2

There's a problem with your regex:

product/(.+)/(.+)/(.+)/(.+)/?$

The .+ part says to match anything as many times as possible. The anything can include /'.

If you used something like

product/([^/]+/)+([^/]+)/?$

breaking this down, we have:

  • product/ which matches product/
  • ([^/]+/)+? - which matches as few as possible, but at least 1, slug/ elements (The [^/] part says match anything that isn't a / )
  • ([^/]+)/?$ which matches a slug, possibly followed by a /, at the end of the line (which is what the $ means.

The post type will then be in $matches[2]

share|improve this answer
up vote 1 down vote accepted

After testing countless different structures i came up with this. It's manual work specifying each category but gives me more control.

If someone comes up with a better solution, please let me know.

function mmp_rewrite_rules($rules) {

//Product structure
$newRules['product/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[4]';
//The above structure only works of the postname is the 5th uri segment

//It's more flexible to confugure the depths for each category below
$newRules['product/televisies/(.+)/(.+)/(.+)/?$'] = 'index.php?products=$matches[3]';
$newRules['product/sports/(.+)/?$'] = 'index.php?products=$matches[1]';

//Category rule
$newRules['product/(.+)/?$']                = 'index.php?pcategory=$matches[1]'; 

return array_merge($newRules, $rules);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.