如何创建自定义URL路由?
2 个回答
- 投票数
-
- 2011-08-19
将其添加到主题的functions.php中,或将其放入插件中.
add_action('init','wpse26388_rewrites_init'); 函数wpse26388_rewrites_init(){ add_rewrite_rule( 'properties/([[0-9] +)/?$', 'index.php?pagename=properties&property_id=$matches [1]', '最佳' ); } add_filter('query_vars','wpse26388_query_vars'); 函数wpse26388_query_vars($ query_vars){ $ query_vars []='property_id'; 返回$ query_vars; }
这将添加一条重写规则,该规则将请求以页面名称
properties
之后的任意数字组合引导到/properties/
,并使用查询varproperty_id
设置.只需确保访问您的永久链接设置页面并保存以刷新刷新规则,即可包含此新规则.在您的
page-properties.php
模板中,get_query_var('property_id')
将返回属性ID(如果已设置),如果未设置则显示默认属性页面.Add this to your theme's functions.php, or put it in a plugin.
add_action( 'init', 'wpse26388_rewrites_init' ); function wpse26388_rewrites_init(){ add_rewrite_rule( 'properties/([0-9]+)/?$', 'index.php?pagename=properties&property_id=$matches[1]', 'top' ); } add_filter( 'query_vars', 'wpse26388_query_vars' ); function wpse26388_query_vars( $query_vars ){ $query_vars[] = 'property_id'; return $query_vars; }
This adds a rewrite rule which directs requests to
/properties/
with any combination of numbers following to pagenameproperties
, with the query varproperty_id
set. Just be sure to visit your permalinks settings page and save to flush rewrite rules, so this new rule will be included.In your
page-properties.php
template,get_query_var('property_id')
will return the property id if it was set, if it's not then show the default properties page.-
这对我来说很接近,但是我需要添加: add_filter('init','flushRules'); 函数flushRules(){ 全局$ wp_rewrite; $ wp_rewrite->flush_rules(); }This was CLOSE to working for me but I needed to add: add_filter('init','flushRules'); function flushRules(){ global $wp_rewrite; $wp_rewrite->flush_rules(); }
- 5
- 2012-11-13
- tooshel
-
@tooshel您绝对不想在每个请求上都刷新规则,这是一项昂贵的操作,会使您的网站慢下来.您只需在激活插件时或仅通过访问永久链接设置页面一次刷新规则.@tooshel you definitely don't want to flush rules on every request, it is an expensive operation and will slow your site to a crawl. you only need to flush rules once, on plugin activation, or just by visiting the permalinks settings page.
- 23
- 2012-11-13
- Milo
-
是的,我明白了...但是当您进行测试时,它确实在那里!Yeah, I get that . . . but when you are testing it's nice that it's in there!
- 1
- 2012-11-14
- tooshel
-
更聪明的重写url正则表达式可能是```^properties/([[0-9] +)/?```否则,它会匹配`example/properties/1`之类的东西A smarter rewrite url regexp might be ```^properties/([0-9]+)/?```. Otherwise it would match something like ```example/properties/1```
- 3
- 2014-12-12
- Ryan Taylor
-
@RyanTaylor您确定吗?测试时,它不会捕获"example/properties/1".@RyanTaylor are you sure about that? it doesn't capture `example/properties/1` when I test it.
- 0
- 2014-12-12
- Milo
-
page-properties.php文件的位置在哪里?我把它放在插件目录中.是对的吗?What is location of page-properties.php file? I put it inside plugin directory. Is that right?
- 0
- 2016-06-30
- Farid Movsumov
-
@FeridMovsumov主题文件总是从当前活动主题目录加载,除非您[添加过滤器](https://developer.wordpress.org/themes/basics/template-hierarchy/#filter-hierarchy)从其他位置加载它们.@FeridMovsumov theme files are always loaded from the current active theme directory, unless you [add a filter](https://developer.wordpress.org/themes/basics/template-hierarchy/#filter-hierarchy) to load them from elsewhere.
- 0
- 2016-06-30
- Milo
-
您好,@ Milo,这是一段漂亮的代码.您是否知道在使用$paged=(get_query_var('paged'))时如何避免冲突?get_query_var('paged'):1;?该函数正在捕获$page(并将其留空),并且我无法继续进行分页.Hello @Milo, this is a beautiful piece of code. Do you know how to make this not clash when using $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ? The function is capturing $page (and leaving it blank) and I'm not able to continue paginating.
- 0
- 2017-08-08
- Jaypee
-
- 2017-05-21
另一种方法:
add_action('init', function() { add_rewrite_rule( '^properties/([0-9]+)/?', 'index.php?pagename=properties&property_id=$matches[1]', 'top' ); }, 10, 0); add_action('init', function() { add_rewrite_tag( '%property_id%', '([^&]+)' ); }, 10, 0);
Another way to do it:
add_action('init', function() { add_rewrite_rule( '^properties/([0-9]+)/?', 'index.php?pagename=properties&property_id=$matches[1]', 'top' ); }, 10, 0); add_action('init', function() { add_rewrite_tag( '%property_id%', '([^&]+)' ); }, 10, 0);
-
接受的答案适用于4.7(和4.8),不确定您为什么认为不行.您的代码本质上是在做同样的事情,`add_rewrite_tag`将查询var与`query_vars`过滤器添加到同一数组.The accepted answer works with 4.7 (and 4.8), not sure why you think it doesn't. Your code is essentially doing the same thing, `add_rewrite_tag` adds the query var to the same array as the `query_vars` filter.
- 2
- 2017-07-07
- Milo
-
@Milo可能对我不起作用,但是我再也没有4.7了,所以我无法检查.我将编辑我的答案.@Milo it probably didn’t work for me, but I don’t have a 4.7 handy anymore so I can’t check. I will edit my answer.
- 0
- 2017-07-08
- Christian Lescuyer
-
@Milo虽然我个人更喜欢重写标签,但是仍然测试了可接受的答案并且它可以工作.不过,只是一些个人口味.@Milo Although I personally prefer rewrite tag, but still tested the accepted answer and it works. Just some personal tastes, though.
- 0
- 2017-07-08
- Jack Johansson
-
@JackJohansson重写标签在您使用时(在永久结构中)是必需的(https://codex.wordpress.org/Function_Reference/add_permastruct).在这种情况下,这只是WordPress从未使用的额外数据.@JackJohansson rewrite tags are necessary when you're using it [in a permastruct](https://codex.wordpress.org/Function_Reference/add_permastruct). It's just an extra bit of data that WordPress never uses in this case.
- 1
- 2017-07-08
- Milo
-
可以将这两个规则添加到同一方法中,最终得到一个更清洁,更有用的功能,以供重新查看并确定是否进行维护工作both rules can be added to the same method, ending up in a cleaner and more useful function to come back to and figure out if doing maintenance work
- 0
- 2018-07-14
- eballeste
我有一个非常特殊的要求,希望我能对此解释得不太混乱.我创建了一个页面模板,其中列出了从外部XML文件获得的一些属性,到目前为止,没有问题,可以说URL如下:
每个属性都有一个链接,该链接应将用户重定向到"单个属性"页面,该页面显示有关该属性的更多信息.我想知道是否有办法制作这样的链接:
其中
123
是属性的ID.因此,如果我有类似properties/some_id
这样的URL,我希望能够加载视图文件(例如single.php
或page.php
文件),但特定于此URL条件.这可能吗?