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 have the following function, it displays the attachments of a post, however it shows the full size, how can i show a limited number of attachments and also a smaller version like a thumbnail.

function page_thumbnails() {
$argsThumb = array(
    'order'          => 'DESC',
    'post_type'      => 'attachment',
    'post_parent'    => $post->ID,
    'post_mime_type' => 'image',
    'post_status'    => null
);
$attachments = get_posts($argsThumb);
if ($attachments) {
    foreach ($attachments as $attachment) {
        apply_filters('the_title', $attachment->post_title);
        echo '<img src="'.wp_get_attachment_url($attachment->ID, 'post-thumbnail', false, false).'" />';
    }

}
}
add_shortcode('page-thumbs', 'page_thumbnails');
share|improve this question
add comment

1 Answer

Use,

wp_get_attachment_image_src( $attachment_id, $size);

Specify the size parameter as follows;

$size (string/array) (optional)

Size of the image shown for an image attachment: either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32). As of Version 2.5, this parameter does not affect the size of media icons, which are always shown at their original size.

Excerpted from the WordPress Codex on wp_get_attachment_image_src

Your final code might look like,

echo '<img src="'.wp_get_attachment_image_src($attachment->ID, 'small').'" />';

Or

echo '<img src="'.wp_get_attachment_image_src($attachment->ID, array(100, 100).'" />';

If you want to specify via array.

Use,

...to specify custom image sizes.

share|improve this answer
    
how about limiting the number of attachments to show, and also it is showing attachments from other pages. since i am using a page to category plugin, i think i am missing a bit of code something like parent child –  Jose David Garcia Llanos Sep 27 '12 at 18:11
add comment

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.