获取媒体库的文件地址

Published
2022-04-26
浏览次数 :  168

 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;
            }
    }

  • 标签1
  • 标签1
  • 标签1
Top