admin管理员组

文章数量:1122832

I am looking for a ReWrite rule or something to achieve this in WordPress:

/mycontent/events/slug_a
/mycontent/events/slug_b
/mycontent/events/slug_c

... should all lead to the page

/mycontent/events/

where I can us the slug as a variable to trigger an wp_query request.

How can I do that?

I am looking for a ReWrite rule or something to achieve this in WordPress:

/mycontent/events/slug_a
/mycontent/events/slug_b
/mycontent/events/slug_c

... should all lead to the page

/mycontent/events/

where I can us the slug as a variable to trigger an wp_query request.

How can I do that?

Share Improve this question edited Aug 20, 2024 at 21:28 wepli23 asked Aug 20, 2024 at 21:28 wepli23wepli23 113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

First add the rewrite rule:

function wpse426504_add_rewrite() {
    add_rewrite_rule( 'mycontent/events/([a-z0-9-]+)[/]?$', 'index.php?eventslug=$matches[1]', 'top' );
}

add_action( 'init', 'wpse426504_add_rewrite' );

function wpse426504_add_query_var( $query_vars ) {
    $query_vars[] = 'eventslug';
    return $query_vars;
} );

add_filter( 'query_vars', 'wpse426504_add_query_var' );

That's the basics of it. If you need to add a new template to handle displaying everything, you'd then hook to template_redirect.

本文标签: url rewritingHow to write ReWrite Rule to cut URL