Skip to content

Instantly share code, notes, and snippets.

@marcosnakamine
Last active March 21, 2017 20:02
Show Gist options
  • Save marcosnakamine/c7b9f2761e4d753f06cc96e689b0e2fd to your computer and use it in GitHub Desktop.
Save marcosnakamine/c7b9f2761e4d753f06cc96e689b0e2fd to your computer and use it in GitHub Desktop.
WordPress - Get source of image gallery by post id
<?php
function get_image_gallery_src( $post_id ) {
$gallery = get_post_gallery( $post_id, false );
$gallery_id = explode( ',', $gallery['ids'] );
$result = array();
foreach ( $gallery_id as $key => $value ) {
$img = wp_get_attachment_image_src( $value, 'full' );
array_push( $result, array(
'title' => get_the_title( $value ),
'src' => $img[0]
));
}
return $result;
}
var_dump( get_image_gallery_src( get_the_ID() ) );
/*
array(2) {
[0]=>
array(2) {
["title"]=>
string(5) "10564"
["src"]=>
string(74) "http://domain.com.br/wp-content/uploads/2017/03/10564.jpg"
}
[1]=>
array(2) {
["title"]=>
string(5) "10562"
["src"]=>
string(74) "http://domain.com.br/wp-content/uploads/2017/03/10562.jpg"
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment