Last active
October 31, 2023 07:03
-
-
Save qutek/7179488567de7fe9c4b8 to your computer and use it in GitHub Desktop.
[Wordpress] [Multisite] Get featured image by blog id on wordpress multisite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment