Get post thumbnail url with multisite
如果要在没有switch\\u blog\\u id的多站点功能上显示材质,请输出图像材质标准功能wordpress fail。为此,我编写了这个小函数,因为找不到交钥匙解决方案。
function multisite_thumb_url($pid, $bid){ //$pid - post id, $bid - blog id
if(isset($pid) && $pid>0){
if(isset($bid) && $bid>0){
global $wpdb;
$curr_blog_id=get_current_blog_id();
$wpdb->set_blog_id( $bid );
$post_thumbnail_id = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = \'_thumbnail_id\' AND post_id = {$pid}" );
$post_thumbnail_metadata = $post_thumbnail_id ? maybe_unserialize( $wpdb->get_var( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = \'_wp_attachment_metadata\' AND post_id = {$post_thumbnail_id}" ) ) : NULL;
if ( ! $post_thumbnail_metadata ){
return false;
}else{
if($bid==1){
$default_post_thumbnail_url = get_blog_option( $bid, \'siteurl\' ).\'/wp-content/uploads/\'.$post_thumbnail_metadata[ \'file\' ];
}else{
$default_post_thumbnail_url = get_blog_option( $bid, \'siteurl\' ).\'/wp-content/uploads/sites/\'.$bid.\'/\'.$post_thumbnail_metadata[ \'file\' ];
}
return $default_post_thumbnail_url;
}
$wpdb->set_blog_id( $curr_blog_id );
}else{
return wp_get_attachment_url( get_post_thumbnail_id($pid) );
}
}else{
return wp_get_attachment_url( get_post_thumbnail_id() );
}
}