收录了这篇文章
当需要访问一个路径调用相应的模板时,可以使用下面的方法:
function rt_rewrite_rules( $wp_rewrite ) { $rt_rules = [ 'orders(.*)$' => 'index.php?rt_custom_page=orders', 'users/([^/]*)/collections/?' => 'index.php?rt_custom_page=collections', 'users/([^/]*)/?' => 'index.php?rt_custom_page=user-center&user_id=$matches[1]', ]; $wp_rewrite->rules = $rt_rules + $wp_rewrite->rules; } add_action( 'generate_rewrite_rules', 'rt_rewrite_rules' ); function rt_add_query_vars($public_query_vars) { $public_query_vars[] = 'rt_custom_page'; $public_query_vars[] = 'user_id'; return $public_query_vars; } add_action( 'query_vars', 'rt_add_query_vars' ); function rt_template_redirect() { global $wp; global $wp_query; global $wp_rewrite; //查询rt_custom_page变量 $rt_custom_page = $wp_query->query_vars['rt_custom_page']; switch ($rt_custom_page) { case 'orders': require_once(TEMPLATEPATH.'/pages/orders.php'); die(); case 'user-center': require_once(TEMPLATEPATH.'/pages/users.php'); die(); case 'collections': require_once(TEMPLATEPATH.'/pages/collections.php'); die(); } } add_action( 'template_redirect', 'rt_template_redirect' ); function rt_flush_rewrite_rules(){ global $pagenow; global $wp_rewrite; if( 'theme.php' == $pagenow && isset( $_GET['activated'] )) { $wp_rewrite->flush_rules(); } } add_action( 'load-themes.php', 'rt_flush_rewrite_rules' ); // add_action('init', 'add_rt_rules'); // function add_rt_rules(){ // add_rewrite_rule( // 'users/collections/?', // 'index.php?rt_custom_page=collections', // 'top' // ); // }
声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。