Wordpress 搜索页面 URL 中的层次分类术语

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

我想创建一个在搜索页面中包含友好 URL 的 WordPress 网站。我有一个名为

cosmetic
的自定义帖子类型,并且该帖子类型有名为
cosmetic_type
cosmetic_region
的自定义分类法。我有一个与
cosmetic_type
相关的分类术语,称为
manicure
,还有一个与
cosmetic_region
相关的分类术语,称为“北”,还有一个子术语,称为“city1”。

当我到达 /search/all 时,我想在搜索页面中查看来自

cosmetic
的所有自定义帖子。

当我到达 /search/manicure/north/city1 时,我想查看与这些术语相关的所有帖子。

当我到达 /search/all/north 时,我想查看与

north
相关的所有帖子。

URL 应该是分层的,但我希望用户在搜索时能够从 2 个不同的分类法中进行选择(如果他愿意的话),这就是为什么我创建了 2 个不同的分类法,而不是 1 个具有多个层次结构的分类法。

我该如何去做呢?我知道我应该使用重写规则,但我以前从未使用过重写规则,并且到目前为止,我尝试实现片段并使其适合我的需求似乎是徒劳的。

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

将其添加到functions.php

    function custom_rewrite_rules() {
    add_rewrite_rule('^search/([^/]+)/([^/]+)/([^/]+)?', 'index.php?post_type=cosmetic&cosmetic_type=$matches[1]&cosmetic_region=$matches[2]&city=$matches[3]', 'top');
    add_rewrite_rule('^search/([^/]+)/([^/]+)?', 'index.php?post_type=cosmetic&cosmetic_region=$matches[1]&city=$matches[2]', 'top');
    add_rewrite_rule('^search/all/?', 'index.php?post_type=cosmetic', 'top');
}
add_action('init', 'custom_rewrite_rules'); 

然后你必须根据URL参数修改搜索页面上的主查询。谢谢!

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