Skip to content

Instantly share code, notes, and snippets.

@qutek
Last active October 31, 2023 07:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qutek/7179488567de7fe9c4b8 to your computer and use it in GitHub Desktop.
Save qutek/7179488567de7fe9c4b8 to your computer and use it in GitHub Desktop.
[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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment