emersonbroga
4/28/2012 - 12:31 AM

Emerson Carvalho.com >> Buscar imagens/anexos do post no WordPress (snippet 1)

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') );
?>