admin管理员组文章数量:1405507
I'd like to add multiple query_var / value pairs to any URL. For example the base URL of a page is:
example/nice-page/
I can already achieve one endpoint like (for example, an on-page subgallery):
example/nice-page/my-gallery/animals/cats
Where my-gallery
is the query_var and animals/cats
is its value. Using this:
add_rewrite_endpoint('my-gallery', EP_PAGES);
add_rewrite_endpoint('my-lightbox', EP_PAGES);
I also need this to work:
example/nice-page/my-gallery/animals/cats/my-lightbox/1234
Where I'd like to register my-lightbox
as an additional endpoint or query_var, because it could be used independently from without my-gallery
, such as:
example/another-page/my-lightbox/2345
Currently I get the entire animals/cats/my-lightbox/1234
as the value. I can't make WP aware that a new query_var starts at my-lightbox
when my-gallery
is alrady present. It seems that rewrite tags are acting on the entirety of the input URL, and endpoints are acting on the end of the input URL, but the regex $ end-anchor is always there. Is it even possible to use the rewrite api to prettify multiple query_vars into a complex URL?
I'd like to add multiple query_var / value pairs to any URL. For example the base URL of a page is:
example/nice-page/
I can already achieve one endpoint like (for example, an on-page subgallery):
example/nice-page/my-gallery/animals/cats
Where my-gallery
is the query_var and animals/cats
is its value. Using this:
add_rewrite_endpoint('my-gallery', EP_PAGES);
add_rewrite_endpoint('my-lightbox', EP_PAGES);
I also need this to work:
example/nice-page/my-gallery/animals/cats/my-lightbox/1234
Where I'd like to register my-lightbox
as an additional endpoint or query_var, because it could be used independently from without my-gallery
, such as:
example/another-page/my-lightbox/2345
Currently I get the entire animals/cats/my-lightbox/1234
as the value. I can't make WP aware that a new query_var starts at my-lightbox
when my-gallery
is alrady present. It seems that rewrite tags are acting on the entirety of the input URL, and endpoints are acting on the end of the input URL, but the regex $ end-anchor is always there. Is it even possible to use the rewrite api to prettify multiple query_vars into a complex URL?
1 Answer
Reset to default 4I also need this to work:
example/nice-page/my-gallery/animals/cats/my-lightbox/1234
Where I'd like to register
my-lightbox
as an additional endpoint or query_var, because it could be used independently from withoutmy-gallery
, such as:example/another-page/my-lightbox/2345
Here's an example of doing it using add_rewrite_rule()
:
add_action( 'init', function() {
// This will let you use get_query_var( 'my-gallery' ).
add_rewrite_endpoint( 'my-gallery', EP_PAGES );
// This will let you use get_query_var( 'my-lightbox' ).
add_rewrite_endpoint( 'my-lightbox', EP_PAGES );
add_rewrite_rule(
'(.?.+?)/(?:my-gallery/(.+?)/my-lightbox/(\d+)|my-gallery/(.+?)|/?my-lightbox/(\d+))/?$',
'index.php?pagename=$matches[1]&my-gallery=$matches[2]&my-lightbox=$matches[3]',
'top'
);
// This is just for testing purposes. You should instead go to the Permalink
// Settings page and click on the Save Changes button to flush the rules. =)
//flush_rewrite_rules();
} );
[EDIT] Alternate version without using add_rewrite_endpoint()
:
add_filter( 'query_vars', function( $qv ) {
// This will let you use get_query_var( 'my-gallery' ).
$qv[] = 'my-gallery';
// This will let you use get_query_var( 'my-lightbox' ).
$qv[] = 'my-lightbox';
return $qv;
} );
add_action( 'init', function() {
add_rewrite_rule(
'(.?.+?)/(?:my-gallery/(.+?)/my-lightbox/(\d+)|my-gallery/(.+?)|/?my-lightbox/(\d+))/?$',
'index.php?pagename=$matches[1]&my-gallery=$matches[2]&my-lightbox=$matches[3]',
'top'
);
} );
And I have tested it on these URLs:
- example/nice-page/my-gallery/animals/cats/my-lightbox/1234
- example/nice-page/my-gallery/animals/cats
- example/nice-page/my-lightbox/1234
..where get_query_var( 'my-gallery' )
returns animals/cats
(for the first and second URLs) and get_query_var( 'my-lightbox' )
returns 1234
(for the first and third URLs).
本文标签: url rewritingHow to have multiple rewrite endpoints in the same URL
版权声明:本文标题:url rewriting - How to have multiple rewrite endpoints in the same URL? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744916944a2632052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_rewrite_rule
– Milo Commented Mar 17, 2018 at 21:47add_permastruct
would help simplify things. You can use rewrite tags to define structures and it will generate the rules for you from that. – Milo Commented Mar 17, 2018 at 23:17