Wordpress 围绕查询构建功能

问题描述 投票:0回答:1

我需要一些有关 WordPress 重写规则和查询的帮助。 我想要的是如果用户访问此链接 https:www.domain.com/catalog

我首先要做的是制定重写规则,这样用户就不会看到 404 错误

add_rewrite_rule(
    '^catalog$', 
    'index.php?expage=$matches[0]', 
    'top'
);

将变量“expage”添加到查询中

add_filter('query_vars', function($vars){
    $vars[] = 'expage';     
    return $vars;       
}, 10, 1);

创建一个返回 true 或 false 的函数

function is_catalog(){
    if(get_query_var('expage') == 'catalog'){
        return true;
    }
}

创建函数并在您想要的地方使用它

if(is_catalog()){
    //do something
}

重写确实有效,我没有看到 404 错误。 但是“is_catalog”功能不起作用。

有人可以帮我解决这个问题吗?

php wordpress url-rewriting
1个回答
0
投票

您需要设置重写规则来捕获字符串“catalog”,然后使用$matches[1]。

add_action ('init', function() {
    add_rewrite_rule(
        '^(catalog)$', 
        'index.php?expage=$matches[1]', 
        'top'
    );    
});

进行此更改后,不要忘记刷新永久链接。

© www.soinside.com 2019 - 2024. All rights reserved.