• Resolved maksibec

    (@maksibec)


    Hello everyone, hallo Arno,

    I am re-working a website using the Twenty Twenty Four theme and blocks extensively for the first time. On the landing page there is a cover image just below the header. Is there a way to change that picture depending on the time of day using Timed Content?

    The website is a about a real life place, the image shows a house or garden and its surroundings. Since there are some quite nice views during the day and especially amazing sunsets and full moons, I would love to show website visitors an image according to the time of day of their visit, let’s say one for the sunrise, one for high noon, one for afternoon, one for the sunset and finally one for night time.

    I understand it could be done with Timed Content albeit it was not built for that specifically and could turn out to be a bit of a hassle. Is that right? Should I try and go for some solution using PHP or JS instead?

    Thanks for you help,

    Max

Viewing 1 replies (of 1 total)
  • Plugin Author Arno Welzel

    (@awelzel)

    Solution with Timed Content

    You may create multiple server side rules for each time of the day with a “daily” frequency and interval of “1”. So one rule shows the conten from 00:00 to 05:59, the next rule from 06:00-11:59, then 12:00-17:59 and so on. So if you want to show 4 different images, you create 4 rules for every image. Every rule has the same start/end date and only a different time for start/end. The repeating pattern should be “daily” with an interval of “1” and you may want to have stopping condition with and end date of 2999-01-01, so it will repeat “forever”.

    In the content you will the use the rules as following in a “classic” block (or if you use a page builder, you need to use a block wich allows to add multiple shortcodes without any separation between them):

    [timed-content-rule id="1"]<HTML for image 1>[/timed-content-rule][timed-content-rule id="2"]<HTML for image 1>[/timed-content-rule][timed-content-rule id="3"]<HTML for image 3>[/timed-content-rule][timed-content-rule id="4"]<HTML for image 4>[/timed-content-rule]

    It’s important to avoid line breaks! Otherwise WordPress will insert a <br> tag between the images, even when they are not displayed.

    Solution using PHP

    A PHP based solution would involve to register a shortcode and build the output in the shortcode handler:

    function my_image_shortcode_handler()
    {
        $output = '';
        $hour = date('H');
        if ($hour >= 0 && $hour < 6)
        {
            $output = '<img src="image-1.jpg" alt="..." />';
        }
        else if ($hour >= 6 && $hour < 12)
        {
            $output = '<img src="image-2.jpg" alt="..." />';
        }
        else if ($hour >= 12 && $hour < 18)
        {
            $output = '<img src="image-2.jpg" alt="..." />';
        }
        else if ($hour >= 18)
        {
            $output = '<img src="image-3.jpg" alt="..." />';
        }
        return $output;
    }
    add_shortcode(
       'my_image_shortcode',
       'my_image_shortcode_handler'
    );
    

    Instead of image-1.jpg use the full URL.

    Instead of using individual if statements you may also use an array with start/end hour (or time) and image URL with alt text and check that in a loop agains the current hour (or time) to determine which array element should be used as output.

    You can then add the shortcode as following:

    [my_image_shortcode]

Viewing 1 replies (of 1 total)
  • The topic ‘Change image by time of day?’ is closed to new replies.