How to get random post attachments WordPress

Get random post attachments from WordPress posts (e.g image attachments).


I’m going to use the following functions. You can add, remove or change parameters if you wish to:

Full Code

Straightforward code with comments.

8 Responses to “How to get random post attachments WordPress”

  1. Frank Zweegers March 17, 2011 at 3:13 pm #

    Als php-nitwit, can you use this code to attach images, rar files etc?

    • quicoto March 17, 2011 at 4:17 pm #

      Well I already posted you can retrieve the attached images.

      And I guess yes you could get the other attachments.

      Try to modify the get_children function parameters 😉

      Let me know if it helps.


  2. Jack June 4, 2011 at 4:08 pm #

    Hi, thank you for posting this code, it was useful!

    I was wondering if there’s a way to take the thumb version of these random images instead of the original one… because I’d load a lot of images and that means a lot of bandwith. Thank you

  3. Jack June 5, 2011 at 3:46 pm #

    oh thanks.
    I think I’m gonna use “timthumb” because my ‘medium’ size images are not a square (even if I use jQuery to resize them) the ‘large’ size are too big (width 750px) and the ‘thumb’ too small… I have to use timthumb it’s the only way I guess…

    thanks again man! 😉

  4. Island Father April 16, 2013 at 9:45 pm #

    Sorry, im new on this… where do i paste this code?

    • Rick April 16, 2013 at 10:31 pm #

      Hi there,

      You should paste this code in one your theme’s files.

      It depends on where you want this to show.

      Check out the WordPress Codex for more information about Theme Development:


Leave a Reply

Add <code> Some Code </code> by using this tags.