我有一个自定义的帖子类型/分类法,它现在运行良好。
我编写了一些自定义重写规则来创建URL,如:
domain.tld/postTypeName/parentTaxonomy/childTaxonomy/postSlug
function custom_rewrite_rules($rules)
{
$newrules = array();
$newrules[\'lessons/(.+)/(.+)/(.+)/?$\'] = \'index.php?lessons=$matches[3]\';
$newrules[\'lessons/(.+)/?$\'] = \'index.php?level=$matches[1]\';
//$newrules[\'lessons/?$\'] = \'index.php?post_type=lessons\';
return array_merge($newrules, $rules);
}
add_filter(\'rewrite_rules_array\', \'custom_rewrite_rules\');
我想做的是创建一个新规则来创建url,如:
domain.tld/postTypeName/parentTaxonomy/home
"home"
它不是自定义帖子类型或分类法的一部分,我想使用已经创建的页面(lessonhome
) 使用此url的自定义模板。
添加了查询变量:
function prefix_register_query_var($vars)
{
$vars[] = \'home\';
return $vars;
}
add_filter(\'query_vars\', \'prefix_register_query_var\');
添加了模板重定向和筛选:
function prefix_url_rewrite_templates()
{
if (get_query_var(\'home\') && is_singular(\'lessons\'))
{
add_filter(\'template_include\', function()
{
return get_template_directory() . \'/custom-page-template.php\';
});
}
}
add_action(\'template_redirect\', \'prefix_url_rewrite_templates\');
添加了新的重写规则:
$newrules[\'lessons/(.+)/home/?$\'] = \'index.php?post_type=page&name=lessonhome&home=yes\';
并且custom\\u rewrite\\u rules方法更改为:
function custom_rewrite_rules($rules)
{
$newrules = array();
$newrules[\'lessons/(.+)/(.+)/(.+)/?$\'] = \'index.php?lessons=$matches[3]\';
$newrules[\'lessons/(.+)/home/?$\'] = \'index.php?post_type=page&name=lessonhome&home=yes\';
$newrules[\'lessons/(.+)/?$\'] = \'index.php?level=$matches[1]\';
//$newrules[\'lessons/?$\'] = \'index.php?post_type=lessons\';
return array_merge($newrules, $rules);
}
add_filter(\'rewrite_rules_array\', \'custom_rewrite_rules\');
但它不起作用-它将我重定向到该页面:
domain.tld/lessonhome
<我做错了什么/错过了什么?