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);
}