最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

permalinks - Extra url paths as variable

programmeradmin0浏览0评论

I have this WordPress pretty permalinks structure:

(where house define post_type)

I want this one url:

(that works like , and parse 2 as "nights" variable and 3 like "people" variable). Is this possible?

I have search but not found any similar question.

Thanks in advance.

I have this WordPress pretty permalinks structure:

https://example/house/name (where house define post_type)

I want this one url:

https://example/house/name/2/3 (that works like https://example/house/name, and parse 2 as "nights" variable and 3 like "people" variable). Is this possible?

I have search but not found any similar question.

Thanks in advance.

Share Improve this question asked Sep 11, 2019 at 11:18 Samuel E. CerezoSamuel E. Cerezo 134 bronze badges 3
  • You can make WordPress aware of custom query string variables using add_rewrite_tag in combination with add_rewrite_rule codex.wordpress/Rewrite_API/add_rewrite_tag – freejack Commented Sep 11, 2019 at 16:09
  • @freejack I've tried add_rewrite_rule unsuccessfully. add_rewrite_rule('house/([^/]+)/([^/]+)/([^/]+)/?', 'index.php?pagename=$matches[1]&nights=$matches[2]&people=$matches[3]', 'top');. I've flushed rewrite rules, update permalinks but not working :( – Samuel E. Cerezo Commented Sep 19, 2019 at 11:26
  • try this rule add_rewrite_rule('house/(.+)/(.+)/(.+)/?$', 'index.php?pagename=$matches[1]&nights=$matches[2]&people=$matches[3]', 'top');. Also remember to use add_rewrite_tagto add the nights and people tags. – freejack Commented Sep 19, 2019 at 12:03
Add a comment  | 

1 Answer 1

Reset to default 0

The correct way to register the rewrite rule and rewrite tag for your case is:

function custom_rewrite_rules() {
    add_rewrite_tag('%nights%', '([^&]+)');
    add_rewrite_tag('%people%', '([^&]+)');
    add_rewrite_rule('house/(.+)/(.+)/(.+)/?$', 'index.php?house=$matches[1]&nights=$matches[2]&people=$matches[3]', 'top');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

The code has been tested and works correctly.

发布评论

评论列表(0)

  1. 暂无评论