Site logo

Archived topic

Change Blocks Based on Season (Date)

7 replies · Started by Dean on February 12, 2022

Viewing posts 1–8 of 8

Hi Tom / David / GP Team

Using the latest version of GeneratePress Premium and GenerateBlocks Pro on a site.

Please see the link in the private section with screen recording which might be more clear.

I am trying to make the homepage dynamic in the sense that some images / texts automatically change depending on the season.

Winter: December-February
Spring: March-May
Summer: June-August
Autumn: September-November

1) Swap background image of a GB Container
2) Swap a GB Grid item (container) containing headline and button for a different one
3) Change the icon of a GB headline block
4) Change the text of a GB headline block

5) How to change some products in a Woocommerce hand-picked product block, or just swap the whole block with different one (not strictly GPP or GB related but if you have any suggestions on how to do this with or without GPP + GBP I would appreciate it)

Is this possible with GPP and GBP? Maybe using Elements, Reusable Blocks or code in an MU plugin?

By the way I do not wish to install any more plugins. Looking for a native or code solution.

Thank you.

Hi there,

automating that is possible but it sounds like a lot of work to be honest.
My personal approach would be to create 4 separate draft Home Pages, one for each season, and then simply swap out my home page as the seasons change.

Hi David.

Thanks for your thoughts. I agree that would be simpler, but unfortunately that's not really an option here. Hence, why I am looking for an automated solution.

The only thing I can think of so far is to duplicate the necessary blocks and then use PHP to inline some CSS on the homepage to display:none based on the date. Although this isn't perfect as there will be a lot of html and images not being used, db queries being run which are then not displaying anything, probably increasing the page size and not great from an SEO perspective to hide too many things I would imagine.

I was hoping there was some clever way I could do something like copy across the content into 4 GP Block Elements and then use some logic to determine which one to display on the homepage content area depending on time of year. Or maybe creating 4 WP reusable blocks and adding some logic to determine which one to use. Is this possible?

Or do you have any other ideas you could share?

Thank you.

I was hoping there was some clever way I could do something like copy across the content into 4 GP Block Elements and then use some logic to determine which one to display on the homepage content area depending on time of year

There is the generate_element_display filter you can use that to set the display of an element using your own custom conditions:

https://docs.generatepress.com/article/generate_element_display/

You will need to write a custom function to determine the Season... not something i have in my snippets bin.

Hi David.

That filter was spot on! Thanks for making me aware of it (need to remember that one for sure!). I didn't set any display rules and then just switched on the appropriate Block Element at the right time of year. GP made it easy peasy. Still get surprised how much thought and detail gets put into this theme.

I just have one small thing left now. There is still a GB headline block on the homepage which displays the current year as you can see in the video I sent.

The only way I can think to do this is to delete the GB headline block and create a PHP shortcode and then add the date with the shortcode and custom css in style.css to style it.

But I was wondering if there is anyway to do this with GPP or GBP? Perhaps some kind of Dynamic Data functionality? Or can I use a shortcode in a headline block at least?

Much appreciated

Hi Dean,

The only way I can think to do this is to delete the GB headline block and create a PHP shortcode and then add the date with the shortcode and custom css in style.css to style it.

Good idea. Here's a quick one -

add_shortcode ('current_year', 'current_year_display');
function current_year_display () {
$year = date('Y');
return $year;
}

Or you can try ones like the snippets provided here - https://divimundo.com/en/blog/copyright-year-wordpress-footer/

You then place the shortcode inside a GB Headline block. This way, you don't have to do the manual styling as it'll use the block's styling. :D

Hi Elvin.

Didn't expect GB headline block to accept shortcodes by default. But worked like a charm. Gosh, GB makes things so simple doesn't it?

Thanks to you both.

No problem. Let us know if you need further help. :D

This archived topic is closed to new replies.