Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Emerson Carvalho.com >> Buscar imagens/anexos do post no WordPress (snippet 1)
<?php
function busca_anexos_post( array $args )
{
$result = array();
$defaults = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
//ID do post que possui os anexos.
'post_parent' => null,
//Tamanhos default de imagens do wordpress thumbnail,medium, large, full
'size' => 'thumbnail'
);
$args = array_merge($defaults,$args);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$image_scr = wp_get_attachment_image_src( $attachment->ID , $args['size'] );
array_push($result, $image_scr);
}
}
return $result;
}
?>
//Exemplo de uso
<?php
$imagens = busca_anexos_post( array( post_parent => 1, 'size' => 'medium') );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.