url rewriting - How do I rewrite URL that has custom parameter

admin2025-06-06  5

I'm stuck on rewriting my url. been searching for days hacking code together but to no avail - So looking for help now.

My url is:

/?type=newsletter

I want the URL to be:


("topic" is a custom taxonomy)

This post seemed the most concise but couldn't get it to work

Help getting this working would make my day.

I'm stuck on rewriting my url. been searching for days hacking code together but to no avail - So looking for help now.

My url is:

https://domainname/topic/ux-process/?type=newsletter

I want the URL to be:

https://domainname/topic/ux-process/type/newsletter

("topic" is a custom taxonomy)

This post seemed the most concise but couldn't get it to work

https://code.tutsplus/articles/custom-page-template-page-based-on-url-rewrite--wp-30564

Help getting this working would make my day.

Share Improve this question edited Nov 20, 2018 at 10:21 Max Stanworth asked Nov 19, 2018 at 21:33 Max StanworthMax Stanworth 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use a rewrite endpoint for this.

The first step is to give your custom taxonomy an endpoint mask when you register the taxonomy:

$args = [
    'rewrite' => [
        'slug' => 'topic',
        'ep_mask' => EP_CATEGORIES
    ],
    // the rest of your args...
];
register_taxonomy( 'topic', array( 'post' ), $args );

The next step is to add the endpoint. This should be hooked to run on init, just like your taxonomy registration code.

add_rewrite_endpoint( 'type', EP_CATEGORIES );

Don't forget to flush rewrite rules after adding this code. The easiest way to do this is by visiting the Settings > Permalinks page.

You should now be able to add type/newsletter onto the end of your taxonomy links. To access the passed value, use get_query_var:

$type = get_query_var( 'type', false );

If the code currently expects to find type in $_GET['type'] and you can't change that, you can manually set that value before the code that tries to access it:

$_GET['type'] = get_query_var( 'type', false );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749166881a316926.html

最新回复(0)