在分层(嵌套)页面上强制重写查询变量

时间:2017-04-30 作者:Ryan Dorn

我想使用带有查询变量的分层(嵌套)页面来提取动态内容。当该页面不是另一个页面的子页面时,我可以使用一些东西,例如:

mysite.com/my-page/1234-my-var/
然而,当我使我的页面成为另一个页面的子页面时,我的query_var 不再工作,例如。

mysite.com/parent-1/parent-2/my-page/1234-my-var/
我的代码

/* Register Query Var
----- */
function my_queryvar_params( $query_v ) {
    $query_v[] = "my_var";
    return $query_v;
};
add_filter(\'query_vars\',  \'my_queryvar_params\');
/* Rewrites
----- */
function setup_filter_rewrites(){
    //add_rewrite_rule(\'^my\\-page\\/([0-9]+)\\/?\', \'index.php?pagename=my-page&my_var=$matches[1]\', \'top\'); // This works when my page has no parent, Returns a 404 when my page is a child/nested
    add_rewrite_rule(\'(.*)\\/my\\-page\\/([0-9]+)\\/?\', \'index.php?pagename=my-page&my_var=$matches[1]\', \'top\'); // This doesn\'t return a 404, but I can\'t fetch my query_var using get_query_var()
    //add_rewrite_rule(\'^parent\\-1\\/parent\\-2\\/my\\-page\\/([0-9]+)\\/?\', \'index.php?pagename=my-page&my_var=$matches[1]\', \'top\'); // This doesn\'t return a 404, but I can\'t fetch my query_var using get_query_var()
};
add_action( \'init\', \'setup_filter_rewrites\' );
/* Force Redirect
----- */
function my_redirect_check() {
    if (isset($_GET[\'my_var\'])) {
        if ($_GET[\'my_var\'] != \'\') {
            if ($_SERVER["HTTPS"]) {$location = \'https://\';}
            else {$location = \'http://\';}
            $location .= $_SERVER[\'SERVER_NAME\'];   
            $location .= strtok($_SERVER[\'REQUEST_URI\'],\'?\');
            $location = trailingslashit($location);
            $location .= $_GET[\'my_var\'];
            $location = trailingslashit($location);                          
            wp_redirect($location); exit;
        };
    };
};
add_action(\'init\',\'my_redirect_check\');
我不确定我的正则表达式是否有错(我通过正则表达式检查器运行了它,它检查出来了),或者我只是缺少了一些明显的东西。

如果您有任何见解,我将不胜感激!

1 个回复
SO网友:Ryan Dorn

以防有人需要:

/* Register Query Var
----- */
function my_queryvar_params( $query_v ) {
    $query_v[] = "my_var";
    return $query_v;
};
add_filter(\'query_vars\',  \'my_queryvar_params\');
/* Rewrites
----- */
function setup_filter_rewrites(){
    add_rewrite_rule(\'^my-parent/my-page\\/([0-9]+)\\/?\', \'index.php?pagename=my-parent/my-page&my_var=$matches[1]\', \'top\'); // As @Milo mentioned, the pagename paramenter needs to reflect the path hierarchy
};
add_action( \'init\', \'setup_filter_rewrites\' );
/* Force Redirect
----- */
function my_redirect_check() {
    if (isset($_GET[\'my_var\'])) {
        if ($_GET[\'my_var\'] != \'\') {
            if ($_SERVER["HTTPS"]) {$location = \'https://\';}
            else {$location = \'http://\';}
            $location .= $_SERVER[\'SERVER_NAME\'];   
            $location .= strtok($_SERVER[\'REQUEST_URI\'],\'?\');
            $location = trailingslashit($location);
            $location .= $_GET[\'my_var\'];
            $location = trailingslashit($location);                          
            wp_redirect($location); exit;
        };
    };
};
add_action(\'init\',\'my_redirect_check\');

相关推荐