我还需要这个来工作:
example.com/nice-page/my-gallery/animals/cats/my-lightbox/1234
我想在哪里注册
my-lightbox
作为附加端点或query\\u var,因为它可以独立于
my-gallery
, 例如:
example.com/another-page/my-lightbox/2345
下面是一个使用
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();
} );
[编辑]备用版本,不使用
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\'
);
} );
我在这些URL上进行了测试:
示例。com/nice page/my gallery/anives/cat/my lightbox/1234示例。com/nice-page/my-gallery/anives/cats示例。com/nice page/my lightbox/1234。。哪里get_query_var( \'my-gallery\' )
退货animals/cats
(对于第一个和第二个URL)和get_query_var( \'my-lightbox\' )
退货1234
(对于第一个和第三个URL)。