带有额外查询变量var
1 个回答
- 投票数
要修改搜索重写规则,您可以插入search_rewrite_rules
过滤器.您可以自己添加与帖子类型匹配的额外重写规则,也可以更改默认的"搜索重写结构"以包含帖子类型,然后重新生成规则(有四个规则:一个标准,一个带有分页的规则和两个供稿).因为WP_Rewrite::generate_rewrite_rules()
在每个"目录级别"生成规则,您将获得/search/[keyword]/section/[post_type]/
,/search/[keyword]/section/
和/search/[keyword]/
.您不需要中间规则,但是保持中间规则也不会受到伤害.
add_filter( 'search_rewrite_rules', 'wpse15418_search_rewrite_rules' );
function wpse15418_search_rewrite_rules( $search_rewrite_rules )
{
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=' );
$search_structure = $wp_rewrite->get_search_permastruct();
return $wp_rewrite->generate_rewrite_rules( $search_structure . '/section/%post_type%', EP_SEARCH );
}
要查看规则,请使用我的重写分析器插件.
To modify the search rewrite rules you can hook into the search_rewrite_rules
filter. You can either add the extra rewrite rules that match post types yourself, or you can change the default "search rewrite structure" to also include the post type and then re-generate the rules (there are four rules: one standard, one with paging and two for feeds). Because WP_Rewrite::generate_rewrite_rules()
generates rules at every "directory level", you will get rules for /search/[keyword]/section/[post_type]/
, /search/[keyword]/section/
and /search/[keyword]/
. You don't need the middle rule, but it won't hurt to keep it in.
add_filter( 'search_rewrite_rules', 'wpse15418_search_rewrite_rules' );
function wpse15418_search_rewrite_rules( $search_rewrite_rules )
{
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=' );
$search_structure = $wp_rewrite->get_search_permastruct();
return $wp_rewrite->generate_rewrite_rules( $search_structure . '/section/%post_type%', EP_SEARCH );
}
To check the rules, use my Rewrite analyzer plugin.
我想知道如何使用
wp_redirect
和template_redirect
钩子将还包含额外查询变量的搜索URL重写为漂亮的永久链接.我从 Nice Search 插件中获取了代码,该插件可以很好地更改
http://example.com?s=africa
放入http://example.com/search/africa
:但是我将选择下拉菜单与 Relevanssi 插件结合使用,以允许访问者将搜索范围缩小到特定的帖子类型.这会添加一个
post_type
查询变量,例如http://example.com?s=africa&post_type=features
.我希望它具有类似http://example.com/search/africa/section/features
的URL.Nice Search代码导致post_type查询var丢失.所以我尝试了以下代码:
但是WordPress现在认为搜索词是
africa/section/features
.有没有办法使搜索字词和查询var都保持在一个很好的永久链接中?
谢谢 西蒙