我有一个我认为非常简单的问题。但由于某种原因,我无法破解它。
我希望现有页面后面的所有内容都作为查询参数定向到该页面。所以
实例com/mypage/this/that/other
实例com/mypage/this/that
实例com/mypage/this
都应该导致
实例com/mypage,查询cpath=/this/that/other(或后面的任何内容)
有什么想法吗?使用标准参考文献只能得到404页
谢谢
我有一个我认为非常简单的问题。但由于某种原因,我无法破解它。
我希望现有页面后面的所有内容都作为查询参数定向到该页面。所以
实例com/mypage/this/that/other
实例com/mypage/this/that
实例com/mypage/this
都应该导致
实例com/mypage,查询cpath=/this/that/other(或后面的任何内容)
有什么想法吗?使用标准参考文献只能得到404页
谢谢
您可以添加重写规则。重写规则是WP用来从友好URL解析查询的正则表达式。但我认为这可能mess things up. WP如何区分/this-is-a-page/this-is-a-child-page/
和/this-is-a-page/this-is-a-parameter/
?
我认为,更好的选择是parse_request
行动在WP加载它认为是查询变量的内容之后,但在它进行查询之前(以及在它决定应该“找不到”请求之前),会调用它此操作挂钩通过引用将WP对象传递给函数。
add_action("parse_request", "my_parse_request");
/**
* @param WP $wp
*/
function my_parse_request(&$wp){
echo "<pre>";
var_dump($wp->query_vars);
die();
}
对于现有页面server.loc/sample-page/
这将输出:array(2) {
["page"]=>
string(0) ""
["pagename"]=>
string(11) "sample-page"
}
正在添加参数。。。server.loc/sample-page/parameter-1/parameter-2/
... array(2) {
["page"]=>
string(0) ""
["pagename"]=>
string(35) "sample-page/parameter-1/parameter-2"
}
自$wp
通过引用传递,您可以更改$wp->query_vars
直接,在检查page_name
实际上是页面并存储参数以供以后使用。请注意page
在里面$wp->query_vars
指页码(如有)。你应该触摸的是pagename
.
源代码parse_request
正在调用wp-includes/class-wp.php
.