我这里有一段代码,但它应该/可能会得到改进。
新建query_vars
创建新的重写规则重定向到特定模板,然后该模板将处理您的参数Create new query vars
function register_gallery_query_vars($vars) {
$vars[] = \'gallery_id\';
$vars[] = \'image_id\';
return $vars;
}
add_filter(\'query_vars\', \'register_gallery_query_vars\');
Add rewrite rule
function custom_rewrite_gallery() {
add_rewrite_rule(\'^gallery/([0-9]+)/([0-9]+)/?\', \'index.php?gallery=$matches[1]&image=$matches[2]\', \'top\');
}
// Change this to run ONLY on plugin activation
// Also have in mind that you need to flush rewrite rules if you change them
add_action(\'init\', \'custom_rewrite_events\');
Redirect to template
function gallery_template_redirection($template)
{
global $wp_query;
if(isset($wp_query->query_vars[\'gallery\'])) { return dirname(__FILE__).\'/gallery.php\'; }
else return $template;
}
add_filter(\'template_include\', \'gallery_template_redirection\', 1, 1);
你的gallery.php
然后可以使用get_query_var()
阅读这些值并使用它们。