最好(也是最复杂的方法)是添加重写规则和重写标签:
添加这样的标签:
http: //codex.wordpress.org/Rewrite_API/add_rewrite_tag
然后是重写规则:
http ://codex.wordpress.org/Rewrite_API/add_rewrite_rule
然后,您将有一个指向类似 YOURDOMAIN.com/YOUR-URL 的指针,在全局 WP Query 中使用适当的变量指向模板文件,您可以使用操作“template_redirect”来执行此操作
所以:
//add the rewrite tag to the WP Query (called on the init action, or earlier)
add_rewrite_tag('%terms-of-service%','([^&]+)');
//add a rewrite rule: (init or earlier)
add_rewrite_rule('^terms-of-service/?','index.php?tos=true','top');
//And then add an action on 'template_redirect':
add_action( 'template_redirect', 'tos_query' );
function tos_query(){
global $wp_query;
if( isset( $wp_query->query_vars['terms-of-service'] ) ){
locate_template( --YOUR URL-- )
}
}
请记住,WordPress 的重写 api 是一个烦人的雷区,您将需要使用
global $wp_rewrite;
$wp_rewrite->flush_rules();
在一切正常之前,可能...
这个插件有一个很好的处理重定向的方法:http ://wordpress.org/plugins/simple-portfolio/ (虽然它是一个帖子类型,而不是一个函数)