mardi 13 septembre 2016

Display random background photograph AND the name of the photographer

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?




Aucun commentaire:

Enregistrer un commentaire