Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on WordPress gallery. I display images with wp_get_attachment_image_src function:

foreach($slideshow_data['attachment_ids'] as $attachment_id) {
    $img_data = wp_get_attachment_image_src( $attachment_id, 'full' );
    echo "<img src=\"{$img_data[0]}\" width=\"{$img_data[1]}\" height=\"{$img_data[2]}\">";
}

Which results in over 140 database queries per page for large galleries. Of course this can be cached, but I feel that this is not correct way.

My question is, am I doing something wrong?

Thank you.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Usually for gallery post types with lots of attachments, you would do it like this:

http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

Inside a "Wordpress Loop":

<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ( $attachments as $attachment ) {
        echo apply_filters( 'the_title' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );
    }
}
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.