lafif-a
9/5/2014 - 9:29 AM

[Wordpress] [Multisite] Get featured image by blog id on wordpress multisite

[Wordpress] [Multisite] Get featured image by blog id on wordpress multisite

/* Get featured image */
if( !function_exists( 'get_the_post_thumbnail_by_blog' ) ) {
	function get_the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
		global $current_blog;
		$sameblog = false;

		if( empty( $blog_id ) || $blog_id == $current_blog->ID ) {
			$blog_id = $current_blog->ID;
			$sameblog = true;
		}
		if( empty( $post_id ) ) {
			global $post;
			$post_id = $post->ID;
		}
		if( $sameblog )
			return get_the_post_thumbnail( $post_id, $size, $attrs );

		if( !has_post_thumbnail_by_blog($blog_id,$post_id) )
			return false;

		global $wpdb;
		switch_to_blog($blog_id);

		$blogdetails = get_blog_details( $blog_id );
		$thumbcode = str_replace( $current_blog->domain . $current_blog->path, $blogdetails->domain . $blogdetails->path, get_the_post_thumbnail( $post_id, $size, $attrs ) );

		restore_current_blog();
		return $thumbcode;
	}

	function has_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL) {
		if( empty( $blog_id ) ) {
			global $current_blog;
			$blog_id = $current_blog;
		}
		if( empty( $post_id ) ) {
			global $post;
			$post_id = $post->ID;
		}

		global $wpdb;
		switch_to_blog($blog_id);

		$thumbid = has_post_thumbnail( $post_id );
		restore_current_blog();
		return ($thumbid !== false) ? true : false;
	}

	function the_post_thumbnail_by_blog($blog_id=NULL,$post_id=NULL,$size='post-thumbnail',$attrs=NULL) {
		echo get_the_post_thumbnail_by_blog($blog_id,$post_id,$size,$attrs);
	}
}