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

url rewriting - Rewrite rule regex help required

programmeradmin1浏览0评论

Hoping you can help with this...

WE have a 'serivces' custom post type that allows a 'service' to have a parent 'service'. Our custom rewrite rule works for top-level services but not child services.

The url structure would be something like the following where 'test-service' and 'test-service-child' are the post names:

  1. consultancy/test-service
  2. consultancy/test-service/test-service-child

Our existing rewrite rule is as follows:

add_rewrite_rule('^consultancy/(.*)/?', 'index.php?post_type=service&name=$matches[1]', 'top');

Can anyone suggest a modification that will get the post name from the end of the url? Eg in the case of the examples above it should match the following

  1. test-service
  2. test-service-child

Any help greatly appreciated. Thanks in advance,

Will

Hoping you can help with this...

WE have a 'serivces' custom post type that allows a 'service' to have a parent 'service'. Our custom rewrite rule works for top-level services but not child services.

The url structure would be something like the following where 'test-service' and 'test-service-child' are the post names:

  1. consultancy/test-service
  2. consultancy/test-service/test-service-child

Our existing rewrite rule is as follows:

add_rewrite_rule('^consultancy/(.*)/?', 'index.php?post_type=service&name=$matches[1]', 'top');

Can anyone suggest a modification that will get the post name from the end of the url? Eg in the case of the examples above it should match the following

  1. test-service
  2. test-service-child

Any help greatly appreciated. Thanks in advance,

Will

Share Improve this question asked Jun 18, 2020 at 10:52 Will FairhurstWill Fairhurst 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Our custom rewrite rule works for top-level services but not child services.

Your rewrite rule, or the query (the second parameter for add_rewrite_rule()) is just missing a service query:

add_rewrite_rule( // wrapped for brevity
    '^consultancy/(.*)/?',

    // Here, add the &service=$matches[1]
    'index.php?post_type=service&name=$matches[1]&service=$matches[1]',

    'top'
);

But, why don't you just use consultancy as the rewrite slug when you register the post type?

register_post_type( 'service', [
    'public'       => true,
    'rewrite'      => [ 'slug' => 'consultancy' ],
    'hierarchical' => true,
    // .. other args here.
] );

That way, you wouldn't need the custom rewrite rule because the above 'slug' => 'consultancy' will set the permalink structure for your "service" posts to consultancy/<post name/slug>.

Can anyone suggest a modification that will get the post name from the end of the URL?

You can use:

  • get_query_var( 'service' ) or get_query_var( 'name' ) to get the complete post name/slug path from the current URL.

    So if the URL is https://example/consultancy/test-service/test-service-child, you'd get test-service/test-service-child.

  • get_queried_object()->post_name to get the actual post name/slug for the queried post, i.e. without the parent path.

    So if the URL is https://example/consultancy/test-service/test-service-child, get_queried_object()->post_name would return test-service-child (note the child).

发布评论

评论列表(0)

  1. 暂无评论