Skip to content

Instantly share code, notes, and snippets.

@delputnam
Created January 6, 2017 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delputnam/ece1350f6079225cc5de3acfee526059 to your computer and use it in GitHub Desktop.
Save delputnam/ece1350f6079225cc5de3acfee526059 to your computer and use it in GitHub Desktop.
Get paths of all image sizes for post attachments
$args = array(
'posts_per_page' => 1,
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post_id,
'post_status' => null,
'post_type' => 'attachment',
);
$attachments = get_children( $args );
foreach( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID );
error_log( $image_attributes[0] );
$img_url_parsed = parse_url( $image_attributes[0] );
foreach( $img_url_parsed as $k => $v ) {
error_log( sprintf( '%s => %s', $k, $v ) );
}
$path_parts = pathinfo( $img_url_parsed['path'] );
$path = $path_parts['dirname'];
error_log( $attachment->ID );
$meta = wp_get_attachment_metadata( $attachment->ID );
foreach( $meta['sizes'] as $size ) {
error_log( $size['file'] );
error_log( sprintf( '%s://%s/%s/%s', $img_url_parsed['scheme'], $img_url_parsed['host'], $path, $size['file'] ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment