Skip to content

Instantly share code, notes, and snippets.

@madeinnordeste
Last active August 29, 2015 14:00
Show Gist options
  • Save madeinnordeste/215355f102f23603f150 to your computer and use it in GitHub Desktop.
Save madeinnordeste/215355f102f23603f150 to your computer and use it in GitHub Desktop.
Wordpress : Get Post Images
<?php
//in theme function.php
//get post images
function post_get_images($ind=NULL){
global $post;
$url_alternative = 'http://www.google.com.br/intl/en_com/images/srpr/logo1w.png';
$list = array();
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg',
'numberposts' => -1,
'post_status' => null,
'order_by' => 'menu_order',
'post_parent' => $post->ID
);
$attachments = get_posts($args);
//jpg
foreach($attachments as $at){
$list[$at->ID] = $at->guid;
}
//bmp
$args['post_mime_type'] = 'image/bmp';
$attachments = get_posts($args);
foreach($attachments as $at){
$list[$at->ID] = $at->guid;
}
//png
$args['post_mime_type'] = 'image/png';
$attachments = get_posts($args);
foreach($attachments as $at){
$list[$at->ID] = $at->guid;
}
//gif
$args['post_mime_type'] = 'image/gif';
$attachments = get_posts($args);
foreach($attachments as $at){
$list[$at->ID] = $at->guid;
}
if(sizeof($list)){
$a = 0;
$images = array();
foreach($list as $k => $v){
$images[$a] = $v;
$a++;
}
unset($list);
if(!is_null($ind)){
if(is_null($images[$ind])){
return false;
}else{
return $images[$ind];
}
}else{
return $images;
}
}else{
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment