我得到了这段代码,可以重写自定义PostType的url。它只适用于第五部分的标题深度。如何调整它以使其接受多个深度?
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/
我尝试添加:
$newRules[\'product/(.+)/(.+)/(.+)/?$\'] = \'index.php?products=$matches[3]\';
但这不起作用,因为在上面的示例结构中浏览child3深度时,它会出现404错误。
当前代码:
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);
}
[更新1]
如果我将代码更改为if\'s和elseif\'s With,它也不会起作用elseif
只添加了最重要的规则,因此我只能浏览posttitle是url中第5段的产品。具有if
添加了所有规则,但我无法浏览深度为2,3和4的类别,但您可以在url位于3,4和5段的深度浏览产品
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);
}