此代码允许您设置自定义元值,如果域名(或子域,如果您编辑代码)与之匹配,则查询将更改为与该帖子匹配。页面模板将仅用于该请求,而不用于通过“正常”URL的请求。
这不会改变该页面上的链接:他们应该去“普通”网站还是留在子域中?
如何在DNS端解决此问题可能是a Server Fault question.
define( \'WPSE4558_STANDARD_SERVER\', \'www.example.com\' );
define( \'WPSE4558_META_KEY\', \'domainname\' );
add_filter( \'request\', \'wpse4558_request\' );
function wpse4558_request( $query_vars )
{
$query_vars[\'is_subdomain_request\'] = false;
if ( WPSE4558_STANDARD_SERVER != $_SERVER[\'SERVER_NAME\'] ) {
$query_vars[\'meta_key\'] = WPSE4558_META_KEY;
// This can also be just the subdomain, if you edit it
$query_vars[\'meta_value\'] = $_SERVER[\'SERVER_NAME\'];
$query_vars[\'is_subdomain_request\'] = true;
}
return $query_vars;
}
add_action( \'parse_query\', \'wpse4558_parse_query\' );
function wpse4558_parse_query( &$wp_query )
{
if ( $wp_query->get( \'is_subdomain_request\' ) ) {
$wp_query->is_home = false;
$wp_query->is_page = true;
$wp_query->is_singular = true;
}
}
add_filter( \'page_template\', \'wpse4558_page_template\' );
function wpse4558_page_template( $template )
{
global $wp_query;
$id = $wp_query->get_queried_object_id();
if( ! $wp_query->get( \'is_subdomain_request\' ) && get_post_meta( $id, WPSE4558_META_KEY ) ) {
// This is a page that has a subdomain attached, but the current request is not via that subdomain
// So use the normal template hierarchy, ignore the page template
$templates = array();
$pagename = $wp_query->get_queried_object()->post_name;
if ( $pagename ) {
$templates[] = "page-$pagename.php";
}
if ( $id ) {
$templates[] = "page-$id.php";
}
$templates[] = "page.php";
$template = locate_template( $templates );
}
return $template;
}