This PHP code enables you to display a random image which changes on every page load.
The solution only uses 3 lines of code in a custom function with WordPress or theme specific hooks like loop_start
or genesis_after_header
etc.
Note: If you’re wanting to rotate images in your themes header image, that functionality is already built into the WordPress customizer so you won’t need this solution which works anywhere you place the code in a template file or in any hook location.
In this example, the code uses 3 images from your child themes images folder and displays a different image every time the page loads.
The images should use the file names:
bg-1.jpg
bg-2.jpg
bg-3.jpg
You can change the filename and extension in the PHP code to match your images.
Demo Video
Here’s the code for registered members:
Was This Tutorial Helpful?