正如韦斯顿所提到的,如果你在WordPress中,并且知道URL来自哪个博客,那么向WordPress索要博客ID。
但如果您真的非常确定,在这种情况下,没有比解析URL更好的方法了,那么您可以这样做:
$url = \'http://myblog.mysite.com/2012/03/16/whatever/\';
$domain = parse_url( $url, PHP_URL_HOST );
// This could be dynamic but good enough for now
$subdomain = str_replace( \'.mysite.com\', \'\', $domain );
// Never use $blog_id for anything, you\'ll break tons of things
$parsed_blog_id = get_id_from_blogname( $subdomain );
// url_to_postid() only works for the current blog
switch_to_blog( $parsed_blog_id );
$post_id = url_to_postid( $url );
restore_current_blog();
参考资料:
http://codex.wordpress.org/Function_Reference/get_id_from_blogname
http://codex.wordpress.org/Function_Reference/url_to_postid