1

I'm trying to use a random image from a certain selection of images as the background photo of a webpage (Wordpress site). The tricky part is I also need to display the name of the photographer who took the photo.

My knowledge of PHP is very very basic, so I have no idea how to approach this. I found a simple script online to pull a random image from a directory, which works great, but what are my options to retrieve the photo credits? Should I create a text file and pull the data from there, somehow? And how could I "connect" the photograph with the name of the photographer?

Here's the script I mentioned to get the images:

<?php

$root = get_stylesheet_directory_uri();
$dir = $root . "/images/bgs/";
$random = rand(1,22);

echo $root . "/images/bgs/bg_" . $random . ".jpg"; ?>

Thank you for your help!

EXTRA CHALLENGE:

That 22 in rand(1,22) is the total number of images in $dir. How do I make it dynamic so that if I add more images to the directory, I don't have to manually update it?

EXTRA EXTRA CHALLENGE

Is there a way to make sure that the same image isn't pulled twice consecutively, or even, say, every 5 times?

Bankitalia
  • 21
  • 4
  • Where are the photo credits stored? – apokryfos Sep 13 '16 at 08:02
  • 1
    You could upload the images via the Wordpress media uploader, then use either the title, caption or description fields to save your credit information. – Lee Sep 13 '16 at 08:02
  • create an associative array `['my_name'=>'my_photo_url']` then use `mt_rand()` to get a random photo along with the photographers name – Dev Man Sep 13 '16 at 08:19
  • Hi @ImmortalDude, thanks for putting me in the right direction, associative arrays is the solution I ended up using. – Bankitalia Sep 16 '16 at 13:40

2 Answers2

1

I can only tell you, how I would do it....

  1. Adding a new user. This user will only be used to upload images. The complete normal way, using the wordpress media uploader in wp-admin.

  2. Adding a hook

    $args = array( 'author' => $author_id, // Replace with author id 'post_status' => 'any', 'post_type' => 'attachment' ); $query = new WP_Query( $args );

taken from here: https://wordpress.stackexchange.com/questions/189967/how-to-display-all-images-uploaded-by-an-author (all credits for this to @jaroslav svetlik :D)

  1. Now you could loop all images uploaded by this user. Use this to get the background image.

  2. Now that you have the ID of the image you could ask for the meta data of the image. FE the description. You could add the authorsname to the description field and output it accordingly.

This is how I would do it. No code example as it seems you know what you're doing. If this answers helps you, please mark it ;-) Thank you!

Extra Challenge is now challange anymore as you are using the regular process and checking for the user. So WP will know if there are more images or not ;-)

Extra Extra Challenge is a bit tricky, maybe I am just thinking to complex? Here is again what I would do:

Setting a new localStorage item with JS (like a cookie but way cooler). As the value I'd set the current chosen image ID. Before setting the background image, i would now check if the id got by the random hook = the saved ID. If yes -> generate new id, if false -> show image AND overwrite the localStorage with the current / new ID.

All the best.

Community
  • 1
  • 1
ThomasB
  • 402
  • 3
  • 14
  • Hi @ThomasB, thank you for your help! I like this solution but I'm not sure about point 3). I mean, once I build a custom query to get the images, how do I edit the above script to get a random image? I do need a bit of code after all :) – Bankitalia Sep 13 '16 at 14:37
  • Hey Bankitalia - for today i am in a rush, let me take a look into it by tomorrow. hope this is soon enough... but italia... you should have end of work too - sooooo see you tomorrow! ;) If I forget about it, just trigger me again. – ThomasB Sep 13 '16 at 14:57
0

As much as @ThomasB's answer was looking good, I ended up exploring the associative arrays idea suggested by @Immortal Dude, which turned out to suit my needs better.

You can read more about the solution here: How to echo values of an associative array returned by function (I had some trouble along the way, so I asked an extra question).

Hope this helps anyone.

Community
  • 1
  • 1
Bankitalia
  • 21
  • 4