WordPress: attachment image with different sizes

I have a small problem with a custom loop in WordPress. I’m using the royal slider (normal jquery version) and want to display the attached images of a post as a slider.

So far, it works with this code:

Read More
<?php
    $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'order' => 'ASC', 'post_status' =>'any', 'post_parent' => $post->ID ); 
    $attachments = get_posts( $args );
    if ( $attachments ) {
            foreach ( $attachments as $attachment ):
                echo '<div class="rsContent">';
                echo wp_get_attachment_image($attachment->ID, 'large', array( 'class' => 'rsImg'));
                $description = $attachment->post_content;
                if ($description):
                    echo '<div class="infoBlock infoBlockLeftBlack rsABlock" data-fade-effect="" data-move-offset="10" data-move-effect="bottom" data-speed="200">';
                    echo $description;
                    echo '</div>';
                endif;
                echo '</div>';
            endforeach;
        }
    ?>

So I get the attached images in fullsize. But now I like to get the attached images in medium and thumbnail size, too, so that I can create a responsive slider.

The way would be, that I can do a picture tag like this:

<picture>
<source srcset="Attachment Thumbnail size" media="(max-width: 400)">
<source srcset="Attachment Medium size" media="(max-width: 900)">
<source srcset="Attachment Full size">
<img srcset="Attachment Full size">
</picture>

Does anybody of you have a quick tip?

Thank you!

Related posts

1 comment

  1. In place of large in echo wp_get_attachment_url($attachment->ID, 'large', array( 'class' => 'rsImg')); use custom-image-size and define the custom-image-size in your functions.php like add_image_size('custom-image-size',730,280,true); and then use regenerate thumbnail plugin and you will get the image size will be changed to 730*280 , visit codex to know more about this function.

Comments are closed.