Revised for clarity:

I want to upload a group of images to the site (through Media so it works through Wordpress) and then have that subset of images used to pull a random image for display either in a header or on a static page of the site. Not in a page, not in a post where galleries are typically used.

I used words like gallery and page interchangeably as I come from a .html coding background.

  • Example of how you might do this posted as an answer, although you don't say where the gallery data is stored, so I've assumed it's a WP gallery in the current post. Please don't take this the wrong way, but along with your recent handful of questions it looks a little as though you are asking people here to build your code for you rather than helping you on points were you are stuck. We all start to learn to code somewhere, and it will help to maintain people's goodwill if you go back to your recent questions and acknowledge answers that have helped you by accepting them and voting them up. – Andy Macaulay-Brook Aug 12 '16 at 16:08
  • Of course I'm new. I also didn't know how the site worked when I started, so I've made a few mistakes. I'm trying to figure it all out and be more clear, but I'm also starting to get the impression that I'm not welcome here. – OscarGuy Aug 12 '16 at 20:07
  • Everyone's welcome, but it is worth spending time looking at the format here and how it works. If your question is well written and answerable within the scope of How to Ask then it will attract up votes which in turn will attract more attention and improve your chances of a good answer. – Andy Macaulay-Brook Aug 12 '16 at 20:09
  • I don't even know how to phrase my question for laymen to understand. How am I going to be able to explain it to people who know this stuff better than I. For all I know I come off as an idiot. BTW: I cannot upvote because I don't have a high enough reputation, so I can accept all the answers I want, but that's all I can do. – OscarGuy Aug 12 '16 at 20:11
  • Accepting is good enough - it marks the question as answered and keeps the site structure tidy. You'll find that some of us will dip in and edit your question to try and make it clearer for others. We'll often try and use a couple of comments to understand the question better and help you fine tune it. People respond more if you can show what code you've tried and present the important parts of it so that the piece you're stuck on is clear. – Andy Macaulay-Brook Aug 12 '16 at 20:13

You can use get_post_galleries to return an array of all the galleries in a post. The data returned includes all the image URLs and IDs, so take your pick of those to suit your case.

$galleries = get_post_galleries( 
    '', // assume current post, if not put a post ID here
    false, // return an array of data rather than the gallery HTML

$images = explode(",",$galleries[0][ids]);
// use the first gallery.  Traversing the array to collate all
// instances of [ids] is left as an exercise for the reader

$index = array_rand( $images );
$imageID = $images[$index];

// then do what you like with the image ID here

get_post_galleries takes two parameters:

  • the customary optional post ID so you can either use it inside a loop or query any post.

  • a boolean to indicate whether you want the whole gallery HTML (if true) or arrays of image IDs and URLs. The URLs are those of the image size specified for the gallery shortcode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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