- This topic has 7 replies, 5 voices, and was last updated 6 years, 7 months ago by
jens.
-
AuthorPosts
-
May 16, 2018 at 1:25 am #576798
J
Hi there,
We created multiple page headers. Super easy with GeneratePress. Thanks!
Now we want a random selection of a page header each time a page is visited. Please note: random selection of the complete page header, not a random selection of the background picture in a certain page header. I read posts in this forum that talk about randomizing the pictures shown in a page header, but could not find any about randomizing complete page headers.
What would be the right approach to realize that?
- Use a plugin-in. If so, any suggestions?
- Write some code to do the job. If so, any starting points?
- Other directions?
Guidance is appreciated. Thank you!
J
May 16, 2018 at 6:34 am #576994David
StaffCustomer SupportHi There, i think this is a question for Tom. Ill pass it his way.
May 16, 2018 at 9:10 am #577239Tom
Lead DeveloperLead DeveloperThat’s a tough one!
You might be able to do this..
add_filter( 'generate_page_header_id', 'tu_random_page_header' ); function tu_random_page_header( $id ) { if ( is_page( 'your-page' ) ) { // Your list of page header IDs. $ids = array( '10', '12', '200' ); $id = array_rand( $ids, 1 ); } return $id; }Not sure if this will work, but it’s worth a try 🙂
May 18, 2018 at 7:41 am #578824J
David, Tom,
Thanks for answering me. A minor modification made it work.
By changing
$idinto$ids[$id]a random selected page header ID is returned.add_filter( 'generate_page_header_id', 'tu_random_page_header' ); function tu_random_page_header( $id ) { if ( is_page( 'your-page' ) ) { // Your list of page header IDs. $ids = array( '10', '12', '200' ); $id = array_rand( $ids, 1 ); } // Return random selected element of the array return $ids[$id]; }J
May 18, 2018 at 10:58 am #579050Tom
Lead DeveloperLead DeveloperInteresting – thanks for sharing the working code! 🙂
November 30, 2018 at 9:51 am #744871chris
HI Tom, I am wondering if you can help.
I have no idea about inserting code etc… but I would like to have random images/pictures again in my header, above the menu, below the logo, like I did when I had pico light installed.
It looks like this code above could help… but I don’t even know how to go about this. Please can you point me in the right direction?
Thanks,
Chris
November 30, 2018 at 5:44 pm #745118Tom
Lead DeveloperLead DeveloperHey Chris,
Would this just be a static image? If so, you can add a new Hook Element into the After Header Content hook.
Then do this:
<?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="" />'; ?>Then check “Execute PHP”, and set your Display Rules.
July 29, 2019 at 1:10 am #969786jens
I tried the code with two images, but it stays static and never changes.
Do I need to change anything in Appearance > Customize > Site Identity ?
That is set to the image that is now always shown.
Ok, I did work it out:
In Appearance > Customize > Site Identity the logo need to be removed.
In Elements > Hooks the setting need to be after_header_content
-
AuthorPosts
- You must be logged in to reply to this topic.