我有一个查询变量show=othertemplate
我可以将其添加到任何URL中,以不同的模板在Wordpress中显示任何帖子、页面或存档,如下所示:mysite.com/[any URL]/?show=othertemplate. 我希望它可以通过干净的URL访问,比如[any URL]/othertemplate 或[any URL]/show/othertemplate.
我尝试通过向query\\u vars过滤器注册query var并向中添加如下代码来实现这一点。htaccess,但Wordpress介入并抛出了404。WP调试栏显示查询正在查找附件=打印。
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\\.php
RewriteRule ^(.*)/othertemplate/?$ /index.php/$1?view=othertemplate [L]
我也尝试过使用Wordpress重写函数,但同样的情况也发生了。function st_myqueryvars($vars) {
$vars[] = \'show\';
return $vars;
}
add_filter(\'query_vars\', \'st_myqueryvars\');
function st_myrewriterules( $wp_rewrite ) {
$newrules = array();
$new_rules[\'^(.*)/othertemplate/?$\'] = \'index.php/$matches[1]?show=othertemplate\';
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_filter(\'generate_rewrite_rules\',\'st_myrewriterules\');
这里有我遗漏的东西吗?Update: 刷新permalinks后,我看到此WP函数重写了/[any URL]/template 到index.php?show=template 而不是index.php?[the URL\'s corresponding query string]&show=template. 我不能“硬编码”像post\\u type=$匹配项这样的查询字符串(1)(&;名称=$匹配项[2]。。。因为它需要对任何具有不同永久链接结构的页面/帖子/归档文件起作用。我该怎么解决这个问题?