获取媒体库的文件地址

Published
April 26, 2022
浏览次数 :  4

 global $post;
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID ); 
    $attachments = get_posts($args);
    if ($attachments) {
            foreach ( $attachments as $attachment ) {
                    // Method #1: Allows you to define the image size
                    $src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
                    if ($src) {echo $src[0];}
                    // Method #2: Would always return the "attached-image" size
                    echo $attachment->guid;
            }
    }

Top