WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Date Specific Stylesheet (4 posts)

  1. inhouse
    Member
    Posted 1 year ago #

    Hello,

    I'm trying to change some CSS based on the current month. I have 4 seasonal background images to change automatically when a certain date.

    I found some code that works but I need to modify it. I'm not sure how to make this work for multiple months. This works if I only use one month but I can't figure out how to enter more than one numeral at a time. Thanks for any help!

    if( date('m') == '9, 10, 11, 12, 1' ) { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/football.css" type="text/css" media="screen" />
  2. inhouse
    Member
    Posted 1 year ago #

    I also thought about trying to add the current month to the body class but I wasn't as successful finding code that would achieve this. If the body had the current month as a class I could target it with CSS. I think that method would be cleaner but I'll take what I can get!

  3. esmi
    Forum Moderator
    Posted 1 year ago #

    I think you're pretty close. Not sure how you want to break the months up, so I'm assuming 4 images = spring, summer, autumn and winter, yes? Try adding the following to your theme's functions.php file:

    // Add special body classes based on season
    if (!function_exists( 'season_body_class' ) ) :
    function season_body_class($classes) {
    	if( date( 'm' ) == '12' || date( 'm' ) == '14' || date( 'm' ) == '2' ) $classes[] = 'winter';
    	elseif( date( 'm' ) == '3' || date( 'm' ) == '4' || date( 'm' ) == '5' ) $classes[] = 'spring';
    	elseif( date( 'm' ) == '6' || date( 'm' ) == '7' || date( 'm' ) == '8' ) $classes[] = 'summer';
    	elseif( date( 'm' ) == '9' || date( 'm' ) == '10' || date( 'm' ) == '11' ) $classes[] = 'autumn';
    	else $classes[] = 'unknown';
    	return $classes;
    }
    endif;
    add_filter('body_class', 'season_body_class' );
  4. inhouse
    Member
    Posted 1 year ago #

    Ahh, that is perfect! Thanks @esmi I really appreciate the help. For anyone else stumbling on to this post, the body class is also required for this to work.

    <body <?php body_class($class); ?>>
    // Add special body classes based on season
    if (!function_exists( 'season_body_class' ) ) :
    function season_body_class($classes) {
    	if( date( 'm' ) == '2' || date( 'm' ) == '3' || date( 'm' ) == '4' ) $classes[] = 'basketball';
    	elseif( date( 'm' ) == '5' || date( 'm' ) == '6' || date( 'm' ) == '7' || date( 'm' ) == '8' ) $classes[] = 'baseball';
    	elseif( date( 'm' ) == '9' || date( 'm' ) == '10' || date( 'm' ) == '11' || date( 'm' ) == '12' || date( 'm' ) == '1' ) $classes[] = 'football';
    	else $classes[] = '';
    	return $classes;
    }
    endif;
    add_filter('body_class', 'season_body_class' );

Topic Closed

This topic has been closed to new replies.

About this Topic