如果您可以指望URI始终遵循相同的模式,例如。/link/###
, 然后,您可以使用查询变量和重写有选择地加载不同的模板(需要刷新重写以进行测试):
/**
* Register a query variable to check against for loading a template.
*
* @param array $vars Registered query variables.
* @return array (Maybe) filtered list of query variables.
*/
function wpdocs_add_query_var( $vars ) {
$vars[] = \'my-template\';
return $vars;
}
add_filter( \'query_vars\', \'wpdocs_add_query_var\' );
/**
* Register an endpoint using the new query variable.
*/
function wpdocs_add_rewrite_rule() {
add_rewrite_rule( \'link/([0-9]+)?$\', \'index.php?my-template=1\', \'top\' );
}
add_action( \'init\', \'wpodcs_add_rewrite_rule\' );
/**
* Load a template based on the presence of a query variable in the request.
*
* @param string $template Template file.
* @return string (Maybe) filtered template file path.
*/
function wpdocs_load_page_template( $template ) {
if ( ! is_admin() ) {
if ( get_query_var( \'my-template\' ) ) {
$template = get_template_directory() . \'/my-template.php\';
}
}
return $template;
}
add_filter( \'template_include\', \'wpdocs_load_page_template\' );