• So, I was looking for a code that could retrieve and display a few recently uploaded images and I found the code below in one relevant thread.

    <?php
    $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_status' => null, 'post_parent' => null );
    $attachments = get_posts( $args );
    if ($attachments) {
    	foreach ( $attachments as $post ) {
    		setup_postdata($post);
    		the_attachment_link($post->ID, false);
    	}
    }
    ?>

    The topic is closed there so I will ask what I need here. The above code successfully pulls the thumbnails of recent images which is great but I need it to pull custom-sized thumbnail. So where do I put the name of the custom sized thumbnail within the code above?

    Also please let me know if I am posting in the wrong forum. Tons of thanks in advance. Cheers !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’d say this is the best forum for your question. We prefer folks start their own topic rather than latch onto other’s, it will result in more people seeing your question and helps the admins keep their stats accurate.

    To answer your question, in addition to getting the attachment link, you need to get the actual attachment object, which is actually a particular post type, so you would be in fact getting a post object. This object contains the image URL in the guid column. From that you can decide if it’s the correct image for your needs, or perhaps locate an alternate sized image.

    Thread Starter southcast

    (@southcast)

    @bcworkz, thanks for reverting back. I am just starting to learn a bit php and pretty much a noob adjusting a few codes here and there. Pardon my ignorance but I am incapable to decipher your explanation, as its a bit too technical for me.

    However if you could please write me back a modified version of the code above, that will be really great. The code is already pulling a “small” sized thumbnail, so it works. I just need to know that if my custom thumbnail size is called “mythumb”, then how is the above code affected in order to pull “mythumb” thumbnail instead of “small” thumbnail it is pulling currently.

    Hope I have been clear enough. Thanks again.

    Moderator bcworkz

    (@bcworkz)

    That was a generic approach that would work for any situation. If certain conditions are met, there are much easier possibilities. If ‘mythumb’ is a registered image size that is always available, you can replace the_attachment_link($post->ID, false); with echo wp_get_attachment_link($post->ID, 'mythumb');

    If it’s not a registered size, but you know the image dimensions and they do not vary, you could replace 'mythumb' with array(200,200) where the numbers are the pixel width,height of the image.

    Thread Starter southcast

    (@southcast)

    Your suggested solutions are flawless. The modified code worked perfectly. Thanks a million for your time @bcworkz.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘help with the code to display thumbnails of recently uploaded images’ is closed to new replies.