permalinks - Extra url paths as variable

I have this WordPress pretty permalinks structure:(where house define post_type)I want this one url:(that works like , a

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745146966a4613689.html

相关推荐

  • permalinks - Extra url paths as variable

    I have this WordPress pretty permalinks structure:(where house define post_type)I want this one url:(that works like , a

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信