Site logo

Archived topic

Randomized Header Images

11 replies · Started by JJ Smith on January 18, 2018

Viewing posts 1–12 of 12

So I want to do randomized header images that work the same way it does if you add your logo to the customize area in GP.

Any ideas on how to do this?

Hi there,

Not sure what you mean by "the same way way it does if you add your logo to the customize area in GP"?

We don't have randomize logo features.

Can you provide an example?

That's what I'm looking for is randomize logos.

Any idea of any plugin that does that?

Thanks,
JJ

So the image would change on refresh, or without refresh (slider)?

Change on refresh.

Thanks,
JJ

You could do something like this in the Before Header Content hook:

<?php
$headers = array(
    'URL to logo 1',
    'URL to logo 2',
    'URL to logo 3',
    'URL to logo 4',
);

$random = $headers[ rand( 0, count( $headers ) -1 ) ];

echo '<img src="' . $random . '" alt="" />';
?>

Awesome :)

The random header image has been working perfectly but now it only works about half the time. Sometimes I get one of the randomized images. Other times I get no header at all. The code I inserted in the Before Header hook follows. I disabled the hook and put one of the header images in via site identity and it works normally. Re-enabled the hook and it's random again.

I had installed a couple of Plugins since I put the hook in but I've disabled them with no change. I did add an additional 150 pages since I the images were reliable.

Site URL is: https://kickngliders.org/wp/

Thanks for any help you can offer!

Bill Stine

<?php
$headers = array(
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_9985.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMGP3557.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMGP3548.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_9677.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_9681.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_9675.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_9590.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_9667.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_9589.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1302.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_3243.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1272.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1275.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1267.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1269.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1266.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1247.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_1251.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0267.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0241.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0244.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0226.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0227.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0218.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0066.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0070.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/IMG_0056.jpg',
'https://kickngliders.org/wp/wp-content/uploads/2018/07/DSCN3626.jpg',
);

$random = $headers[ rand( 0, count( $headers ) -1 ) ];

echo '';
?>

That looks ok to me. Are you sure all of those URLs are working correctly?

Color me embarrassed! I recently installed and used the plugin, Media Library Folders for WordPress, to organize my media and moved all those images into their own folder. The plugin does update the URL in pages that use the file. I didn’t account for the fact that the plugin apparently does not update the hooks code. What baffled me was that it worked ok about half the time. I suppose it might have something to do with caching? Anyway, my bad.

It’s working perfectly now. Thanks, again, for your rapid and thoughtful response!

Bill

Glad you got it working :)

This archived topic is closed to new replies.