flush_rules(); } } // 添加自定义URL重写 function gdk_sitemap_html_custom_rewrite_rule() { add_rewrite_rule('^sitemap(.*?)\.html$','index.php?sitemap=gdkk$matches[1]','top'); } function gdk_sitemap_html_insert_query_vars( $vars ){ array_push($vars, 'sitemap'); return $vars; } function gdk_sitemap_html_cancel_redirect( $redirect_url ) { $api_type = get_query_var('sitemap'); if ( !empty($api_type) ){ return false; }else{ return $redirect_url; } } function gdk_sitemap_html_api_handlers( $template ){ $hook = explode('-', get_query_var( 'sitemap' ) ); if( isset( $hook[0] ) && $hook[0] === 'gdkk' ){ if( isset( $hook[1] ) ){ status_header(404); header('HTTP/1.0 404 Not Found'); $GLOBALS['wp_query']->set_404(); include( get_query_template( '404' ) ); exit; } $sitemap = get_transient('gdk-sitemap-html'); if( false === $sitemap || empty( $sitemap ) ){ $sitemap = gdk_create_html_sitemap(); set_transient( 'gdk-sitemap-html', $sitemap ); } header("Content-type: text/html"); $sitemap; return; } return $template; } add_action( 'init', 'gdk_sitemap_html_custom_rewrite_rule' , 10, 0); add_filter( 'query_vars', 'gdk_sitemap_html_insert_query_vars' ); add_filter( 'redirect_canonical', 'gdk_sitemap_html_cancel_redirect' ); add_action( 'wp_loaded', 'gdk_sitemap_html_flush_rules' ); add_filter( 'template_include', 'gdk_sitemap_html_api_handlers', 99 ); function gdk_create_html_sitemap() { ?>