- This topic has 20 replies, 3 voices, and was last updated 3 years, 6 months ago by
David.
-
AuthorPosts
-
August 7, 2022 at 3:10 am #2305598
Joey
I know this question has been addressed a lot, but I’m having trouble making it work. I have a page hero on my featured page that I created with a header element. I added a custom image, and put text in it, as in the link below.
I also created another page hero with a separate header element, having a different image and different text, and set display rules for Front Page as well. If possible, I’d like a different page header to appear every time a user goes to the front page.
In a previous post, someone had mentioned that they used the following code to make it work, but when I do it it only shows the most recent header, regardless of how many times I reload the page.
add_filter( 'generate_page_header_id', 'tu_random_page_header' ); function tu_random_page_header( $id ) { if ( is_front_page() ) { // Your list of page header IDs. $ids = array( '6013', '8247' ); $id = array_rand( $ids, 1 ); } // Return random selected element of the array return $ids[$id]; }Just to clarify, “page header ID” refers to the code in the URL when I am editing the header element? I’ve got two there so far, but it only shows the most recent header element I created. Any help would be appreciated.
August 7, 2022 at 5:56 am #2305711David
StaffCustomer SupportHi there,
are you using a Block Element – Page Hero ? Or a Header Element ?
August 7, 2022 at 11:21 pm #2306376Joey
I’m using a header element. I didn’t know I could do page heroes with a block element, but then it doesn’t merge with the header. But if that was better, I could try to do it that way.
August 8, 2022 at 3:41 am #2306578David
StaffCustomer SupportOK, thats fine.
Try this:
add_filter( 'generate_element_display', function( $display, $element_id ) { // Your arry of element IDs $el_array = array( 3137, 3032, 2352, 2523, 2523 ); $rand_element = array_rand( $el_array, 1 ); unset( $el_array[$rand_element] ); if ( in_array( (int)$element_id, $el_array ) ) { $display = false; } return $display; }, 10, 2 );Update the
$el_arrayarray with your IDs.
And make sure all of the Header Elements are Published and have their display rules set to the same location.August 8, 2022 at 11:02 pm #2307463Joey
Thank you David, it works. Sorry, I had assumed that the page hero used the same code as the header.
I have one question: When it generates a random page hero based on the
$el_arrayarray, one of the options that sometimes appears is nothing. In other words, sometimes no page hero at all is randomly selected. Is there a way to eliminate that part so that there is always a (random) page hero?August 9, 2022 at 1:55 am #2307585David
StaffCustomer SupportI was kinda expecting something to not work lol
Can i see the page ?August 9, 2022 at 3:17 pm #2308308Joey
Yes, the link is below. Well, every once in a while it works the first time. Thank you.
August 10, 2022 at 5:08 am #2308753David
StaffCustomer SupportCan you share a screenshot of what i should be seeing ? I get the impression its not appearing for me
August 10, 2022 at 8:51 pm #2309467Joey
Sure, sometimes I get a page hero like this, or this, but sometimes it just appears as the header without any hero, like this. I’m not sure if this is the reason, but sometimes I have to reload the page 4 or 5 times before the empty page hero appears, since it’s random (I have 4 separate banners, 2 of them are dummies). Not a huge deal, but it would be nice if I could remove the empty page hero so that users always have an advertisement at the top.
August 11, 2022 at 3:28 am #2309800David
StaffCustomer SupportAhh – i can see the heroes now – thats cool 🙂
Odd that occasionally it displays none. Can’t see why it would, theres no global variables being used so its not as if this:unset( $el_array[$rand_element] );is permanently unsetting values from the array. If that were the case, after all the values had been unset there would be no element shown ever again.I wonder if this method would work:
1. All Header Elements should have NO display rule location set.
2. Use this snippet instead of the current one:
add_filter( 'generate_element_display', function( $display, $element_id ) { // Your arry of element IDs $el_array = array( 3137, 3032, 2352, 2523, 2523 ); $rand_element = array_rand( $el_array, 1 ); if ( is_front_page() && $rand_element == $element_id ) { $display = true; } return $display; }, 10, 2 );This will randomly select one and set its display to True whilst viewing the Front Page.
August 12, 2022 at 1:50 am #2310689Joey
David, what do you mean by “NO display rule location set”?
I tried just deleting the location rule on those 4 headers, and switching out the code (with the correct IDs), but it has no effect—it doesn’t display any headers on the front page, even when I refresh. If I leave the location rule, it just displays the most recent header, and doesn’t cycle through them.
I also tried keeping the original code and deleting all the other custom PHP on the site, just in case. But it still has the same effect. Weird.
It doesn’t have to be completely random, it could just cycle through the 4 headers in the same order. I don’t know if that would make a difference.
August 12, 2022 at 5:56 am #2310850David
StaffCustomer SupportDavid, what do you mean by “NO display rule location set”?
this is correct:
I tried just deleting the location rule on those 4 headers,
I am going to ask one of the team to take a look at my code here:
https://generatepress.com/forums/topic/random-page-header-on-featured-page-with-text/#post-2306578
Might be something i am missing, or it may just be a server caching thing.
August 12, 2022 at 7:00 pm #2311477Joey
Okay, let me know. I’ll leave the page with the original code and the page hero elements displaying Front Page for now. Thank you.
August 14, 2022 at 3:45 am #2312335David
StaffCustomer SupportI have passed the topic on to Tom, so see if has any insight. We’ll let you know 🙂
August 14, 2022 at 12:31 pm #2312773Tom
Lead DeveloperLead DeveloperStrange, my guess is that it has something to do with caching (plugin or server).
Are you still using the first snippet David provided? I’m only seeing “Header 4” no matter how many times I refresh.
If you’re using that first snippet, can you try adding:
var_dump($rand_element);After:
$rand_element = array_rand( $el_array, 1 );This will output some debugging info on the page – what does it output when no hero is displayed?
-
AuthorPosts
- You must be logged in to reply to this topic.