Last active
August 29, 2015 14:00
-
-
Save madeinnordeste/215355f102f23603f150 to your computer and use it in GitHub Desktop.
Wordpress : Get Post Images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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