Site logo

[Support request] Random page header on featured page (with text)

Home Forums Support [Support request] Random page header on featured page (with text)

Home Forums Support Random page header on featured page (with text)

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #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.

    #2305711
    David
    Staff
    Customer Support

    Hi there,

    are you using a Block Element – Page Hero ? Or a Header Element ?

    #2306376
    Joey

    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.

    #2306578
    David
    Staff
    Customer Support

    OK, 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_array array with your IDs.
    And make sure all of the Header Elements are Published and have their display rules set to the same location.

    #2307463
    Joey

    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_array array, 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?

    #2307585
    David
    Staff
    Customer Support

    I was kinda expecting something to not work lol
    Can i see the page ?

    #2308308
    Joey

    Yes, the link is below. Well, every once in a while it works the first time. Thank you.

    #2308753
    David
    Staff
    Customer Support

    Can you share a screenshot of what i should be seeing ? I get the impression its not appearing for me

    #2309467
    Joey

    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.

    #2309800
    David
    Staff
    Customer Support

    Ahh – 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.

    #2310689
    Joey

    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.

    #2310850
    David
    Staff
    Customer Support

    David, 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.

    #2311477
    Joey

    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.

    #2312335
    David
    Staff
    Customer Support

    I have passed the topic on to Tom, so see if has any insight. We’ll let you know 🙂

    #2312773
    Tom
    Lead Developer
    Lead Developer

    Strange, 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?

Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.