Support » Fixing WordPress » Show widget based on day of the week

  • This is for an up and coming WP theme I am building…

    Is it possible to display a particular widget depending on the day of the week? For example, we want to display the hours of operation for the current day in the footer. I figure I could setup seven widgets (one for each day) but would need some conditional logic to say when to show one and not the others.

    I found a couple of plugins that MAY accomplish this but it was not clear so I have reached out on those support areas.

    I prefer to not use a plugin and would rather hardcode something in functions.php if at all possible. Searching for this type of function is difficult as publish times for posts/pages most commonly appears and that is not what I am looking for.

    Thank you in advance for any guidance you can provide!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi!

    Not sure if you seen this plugin or not:
    https://wordpress.org/plugins/widget-logic/

    It may be what you are possible looking for. If you you may be able to fork the code and modify it to suit your needs!

    Hope that helps you out!

    The “easy” way to do this is to create yoru own Widget class.

    https://codex.wordpress.org/Widgets_API

    That way you can make sure that the output matches the day of the week with a very easy PHP function. Something like a switch( date( 'w' ) ) would work for that.

    Thread Starter Tim Derouin

    (@tderouindesign)

    @jose Castaneda, I have a simlar post on the support forum for that plyugin already but have yet to hear anything. From what I read it wasnt apparent that the plugin was capable of accomplishing what I am looking to do.

    @catacaustic, thank you for that piece of advice! This is a low budget job so the time it would take to implement the widget class may be a bit too much but I appreciate the input! At the very least, I will explore that codex page for future reference.

    Thanks to you both!

    @tderouindesign Did you end up resolving this? I want to do the same thing – in the header. I was looking at creating my own plugin because I didn’t see one that does this. There are plenty that show all hours, or that say “It’s 3:00pm We’re Open Now” but not one that says “It’s Monday – Our Hours are…”
    Would love to know if you found a solution.
    Thanks!

    Thread Starter Tim Derouin

    (@tderouindesign)

    @taraclaeys I was able to find a solution via the Dynamic Widgets plugin (https://wordpress.org/plugins/dynamic-widgets/).

    A widget was created for each day of the week. Then, while in the Dynamic Widgets settings area select Days and select the appropriate day for each widget to be displayed.

    This requires calling on all 7 widgets (one for each day) in the same div of your header.php document but per your edited plugin settings only one widget will show daily.

    For example, your header would need something like this…

    Open today: <?php if ( dynamic_sidebar('Hours: Sunday') ) : else : ?><?php endif; ?>
                <?php if ( dynamic_sidebar('Hours: Monday') ) : else : ?><?php endif; ?>
                <?php if ( dynamic_sidebar('Hours: Tuesday') ) : else : ?><?php endif; ?>
                <?php if ( dynamic_sidebar('Hours: Wednesday') ) : else : ?><?php endif; ?>
                <?php if ( dynamic_sidebar('Hours: Thursday') ) : else : ?><?php endif; ?>
                <?php if ( dynamic_sidebar('Hours: Friday') ) : else : ?><?php endif; ?>
                <?php if ( dynamic_sidebar('Hours: Saturday') ) : else : ?><?php endif; ?>

    As well as this in your functions.php doc…

    register_sidebar( array(
    		'name' => 'Hours: Sunday',
    		'id' => 'hours-sunday',
    		'before_widget' => '<span style="clear:none;">',
    		'after_widget' => '</span>',
    		'before_title' => '',
    		'after_title' => '',
    	) );
    	
    	register_sidebar( array(
    		'name' => 'Hours: Monday',
    		'id' => 'hours-monday',
    		'before_widget' => '<span style="clear:none;">',
    		'after_widget' => '</span>',
    		'before_title' => '',
    		'after_title' => '',
    	) );
    
    etc.

    Hope that helps!

    I found that plugin too! Yay! Day made. Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Show widget based on day of the week’ is closed to new replies.