我有一个页面示例(ID=443,slug=示例),通过Dashboard->Settings->Reading
, 作为我的WP网站的主页。我还配置了以下重写规则:
[(example)/(view-section)] => index.php?pagename=$matches[1]&foo=1&bar=2
我在示例页面中有一个短代码,它应该根据foo
和bar
查询变量。问题是在访问URL时没有正确设置查询变量/example/view-section
(及其他相关内容,见下文)。我正在使用一个测试插件来调试这个问题。完整代码如下:
<!-- language: lang-php -->
<?php
function test_shortcode() {
global $wp_rewrite;
echo \'<pre>\' . \'foo: \' . get_query_var(\'foo\') . \'</pre>\';
echo \'<pre>\' . \'bar: \' . get_query_var(\'bar\') . \'</pre>\';
echo \'<pre>\' . print_r($wp_rewrite->rules, true) . \'</pre>\';
}
add_shortcode(\'foobar\', \'test_shortcode\');
function test_rules() {
$rules = get_option(\'rewrite_rules\');
if (!isset($rules[\'(example)/(view-section)\'])) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
add_action(\'wp_loaded\', \'test_rules\');
function test_rewrite_rules($wprules) {
$rules = array(\'(example)/(view-section)\' => \'index.php?pagename=$matches[1]&foo=1&bar=2\');
return $rules + $wprules;
}
add_action(\'rewrite_rules_array\', \'test_rewrite_rules\');
function test_query_vars($query_vars) {
array_push($query_vars, \'foo\');
array_push($query_vars, \'bar\');
return $query_vars;
}
add_filter(\'query_vars\', \'test_query_vars\');
有三种情况给了我不同的结果:转到/示例/视图部分重定向到/且查询变量值为空page_id 而不是pagename
或page
: /指数php?page\\u id=443(&U);foo=1(&;bar=2,则page_id
将删除查询变量,并再次显示默认主页我想知道WP为什么会出现上述行为,如果可能的话,如何使第一种情况至少与第二种情况类似。
请注意,如果示例页面未配置为主页,则案例1和3与案例2相同。
感谢您的帮助,如有必要,我很乐意提供更多细节。
EDIT
该问题最初包括指向正在运行的WP实例的链接以及该问题的一个示例。然而,我不能让那个实例永远运行下去,所以我删除了链接,并试图用文字来解释情况。