Digital experiences for all disciplines
New Landing › How can we help? › Cardinal › Body Class with filter
New Landing › How can we help? › Cardinal › Body Class with filter
- This topic has 7 replies, 2 voices, and was last updated 9 years by Rui Guerreiro – SUPPORT.
-
Posted in: Cardinal
-
April 30, 2015 at 3:10 pm #170557
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!
April 30, 2015 at 3:20 pm #170566Hi,
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
April 30, 2015 at 3:26 pm #170567Throwing 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
April 30, 2015 at 3:34 pm #170572Replace this line
function getSeasonCssClass() {by this one
function getSeasonCssClass($classes) {I’m afraid I can’t keep support if it doesn’t work.
-Rui
April 30, 2015 at 3:55 pm #170588Rui,
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!
April 30, 2015 at 4:04 pm #170594Thanks 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;
placereturn "testing";
To check if the value of the month is right use var_dump($month);
Good luck
-Rui
April 30, 2015 at 4:29 pm #170605Rui,
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
April 30, 2015 at 4:38 pm #170608Great. glad it’s sorted. Some problem reaching the array in the first alternative.
-Rui -
Posted in: Cardinal
You must be logged in to reply to this topic.