我无法使查询重写按预期工作。期望的结果是将请求wpsite.com/foo/bar/2/重写为wpsite.com/foo/bar/?my_id=2。functions.php// Add custom URL parametersfunction add_custom_query_var( $vars ){ $vars[] = "my_id"; return $vars;}add_filter( 'query_vars', 'add_custom_query_var' );// Add rewrite for my_idfunction custom_rewrite_basic() { add_rewrite_rule('^foo/bar/([0-9]+)/?', 'foo/bar/?my_id=$matches[1]', 'top');}add_action( 'init', 'custom_rewrite_basic' );bar.php$my_id = filter_input( INPUT_GET, "my_id", FILTER_SANITIZE_NUMBER_INT );var_dump($my_id);当我打电话给/foo/bar/?my_id=2我2时/foo/bar/2/,我看不到。更新刷新缓存后,结果得到了改善。但是,无论传递的参数(3,256205)如何,$my_id都返回为1。wpsite.com/foo/bar/?my_id=2 表演 2wpsite.com/foo/bar/2/ 表演 1
1 回答
RISEBY
TA贡献1856条经验 获得超5个赞
这可以通过更改$matches[1]为来解决$1:
// Add rewrite for my_id
function custom_rewrite_basic()
{
add_rewrite_rule('^foo/bar/([0-9]+)/?', 'foo/bar/?my_id=$1', 'top');
}
add_action( 'init', 'custom_rewrite_basic' );
当然还有刷新缓存。
- 1 回答
- 0 关注
- 158 浏览
添加回答
举报
0/150
提交
取消