New Landing How can we help? Cardinal Body Class with filter

Viewing 8 posts - 1 through 8 (of 8 total)
  • Posted in: Cardinal
  • #170557
    Ian
    Member
    Post count: 121

    This is probably beyond your normal support perimeters but I am hoping someone can help me out here. I want to add the “Season” to my body class for some CSS tweaking of my site.

    I can successfully add the date to my body class.

    Here is the working code:

    add_filter( 'body_class', 'my_class_names' );
    function my_class_names( $classes ) {
    	// add 'class-name' to the $classes array
    	$classes[] = date('M');
    	// return the $classes array
    	return $classes;
    }

    What I want to add is the season. I have found the following code but have been unable to put the whole thing together.

    Not working code:

    function getSeasonCssClass() {
      $month = date('n'); // current month number without leading 0
      $season = array(
        1 => 'winter',
        2 => 'winter',
        3 => 'winter',
        4 => 'spring',
        5 => 'spring',
        6 => 'spring',
        7 => 'summer',
        8 => 'summer',
        9 => 'summer',
        10 => 'autumn',
        11 => 'autumn',
        12 => 'autumn',
      );
      return $season[$month];
    }

    Would appreciate any help, thanks!

    #170566
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    Try this one instead.

    
    add_filter( 'body_class', 'getSeasonCssClass' );
    
    function getSeasonCssClass() {
      $month = date('n'); // current month number without leading 0
      $season = array(
        1 => 'winter',
        2 => 'winter',
        3 => 'winter',
        4 => 'spring',
        5 => 'spring',
        6 => 'spring',
        7 => 'summer',
        8 => 'summer',
        9 => 'summer',
        10 => 'autumn',
        11 => 'autumn',
        12 => 'autumn',
      );
      $classes = $season[$month];
      return $classes;
    }

    Hope it helps.

    -Rui

    #170567
    Ian
    Member
    Post count: 121

    Throwing a couple of errors:

    Warning: array_unique() expects parameter 1 to be array, string given in /home/isterne/public_html/sandboxes/sandbox25/wp-includes/post-template.php on line 721

    Warning: join() [function.join]: Invalid arguments passed in /home/isterne/public_html/sandboxes/sandbox25/wp-includes/post-template.php on line 516
    class=””>

    -Ian

    #170572
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Replace this line
    function getSeasonCssClass() {

    by this one
    function getSeasonCssClass($classes) {

    I’m afraid I can’t keep support if it doesn’t work.

    -Rui

    #170588
    Ian
    Member
    Post count: 121

    Rui,

    Thanks for the help, still not working, I will keep trying to figure this one out.

    By the way I am using a great plugin Code Snippets for managing snippets that I would highly recommend to any of your users.

    Thanks again!

    #170594
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Thanks for the info.
    Just one thing the problem can be in the php of the existing function. try to return a fixed value just to see if it works.

    instead return $classes; place return "testing";

    To check if the value of the month is right use var_dump($month);

    Good luck

    -Rui

    #170605
    Ian
    Member
    Post count: 121

    Rui,

    Just for your info got this working not exactly elegant but works!

    add_filter('body_class','browser_body_class');
    
    function browser_body_class($classes = '') {
    	$month = date('n');
    
    	if($month == 1) $classes[] = 'winter';
    	elseif($month == 2) $classes[] = 'winter';
    	elseif($month == 3) $classes[] = 'winter';
    	elseif($month == 4) $classes[] = 'spring';
    	elseif($month == 5) $classes[] = 'spring';
    	elseif($month == 6) $classes[] = 'spring';
    	elseif($month == 7) $classes[] = 'summer';
    	elseif($month == 8) $classes[] = 'summer';
    	elseif($month == 9) $classes[] = 'summer';
    	elseif($month == 10) $classes[] = 'autumn';
    	elseif($month == 11) $classes[] = 'autumn';
    	elseif($month == 11) $classes[] = 'autumn';
    
    	return $classes;
    }

    -Ian

    #170608
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Great. glad it’s sorted. Some problem reaching the array in the first alternative.
    -Rui

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.