Site logo

Archived topic

Random page header on featured page (with text)

20 replies · Started by Joey on August 7, 2022

Viewing posts 16–21 of 21

Weird, it didn't do that before. I tried it with Edge and Chrome, and it's doing the same thing you mentioned (Header 4). When I purged the dynamic cache (on the SiteGround website), it changed the header (but still gets stuck on one header). I tried disabling the dynamic cache completely (with a SiteGround plugin), and now it cycles through the 4 headers on all browsers, so you should be able to see it now? However, I still get the issue of an empty header randomly appearing. I added var_dump($rand_element);.

edit:
Sorry Tom, I didn't answer your question. Yes, I have the original snippet that Dave gave me. This is what I get when the empty header appears:
int(0) int(2) int(3) int(3) int(2) int(1) int(3) int(3) int(3) int(0) int(3) int(2)

The second time it appeared I got this:
int(0) int(1) int(2) int(0) int(0) int(3) int(2) int(2) int(3) int(1) int(0) int(1)

The third time it appeared I got this:
int(3) int(3) int(1) int(3) int(3) int(0) int(2) int(3) int(1) int(0) int(3) int(3)

Every time it is different.

I don't have a lot of plugins on my site. The only ones related to caching are SiteGround Optimizer (which I just installed and is set to "Caching: Disabled").

Caching will definitely mess with anything "random", as random generation of anything goes against the idea of caching.

To fix this, let's go with David's solution here: https://generatepress.com/forums/topic/random-page-header-on-featured-page-with-text/#post-2309800

We're going to tweak the code a little:

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() && $el_array[ $rand_element ] == $element_id ) {
        $display = true;
    }
 
    return $display;
    
}, 10, 2 );

That should do the trick I think.

Let me know :)

Okay, that's working Tom. It didn't do anything for a couple minutes, even after clearing the cache—I was worried. Thank you both for all your help.

Glad we could help! :)

I have no idea why, but out of nowhere I'm starting to get the same issue again with Tom's new code—no page header randomly appears. I can only assume this is an issue with SiteGround's caching, though supposedly all caching is turned off. I'm going to try hosting it myself with Digital Ocean next time I need to give them money, and see if that gives me more control. In the mean time, I don't want to waste your guys' time, so I'll just leave Dave's original code for now. If I put 10 or 20 different header elements (i.e. repeat the same header elements with different page IDs), the non-header doesn't show up much. So that might be a good-enough solution for now. The other option would be to change the header manually. But I just wanted to follow up in case anyone else is having this issue. Thanks again for your help.

I think caching is always going to cause a bit of a headache here.
You could ask the Host if they can set up a cron job to clear the cache on the affected pages.

But thank you for the kind feedback - much appreciated :)

This archived topic is closed to new replies.