标题图像会根据季节自动更改

时间:2020-09-29 作者:sfera

几天来一直在试图找出脚本的错误。作为使用“primer”的主题。重点是使标题图像在每个季节都会自行改变。在页面标题处显示fall 就是这样,即使我测试不同季节的不同月份,也不会发生任何事情。也许有人能帮我解决这个问题。

功能

    add_filter(\'masthead\',\'site-header\');
function seasonal_site_header ($classes = \'\') {
   $hemisphere = \'northern\';
   $m = date(\'m\');
    
   $northern=array(
     \'summer\' => array(6, 7, 8),
     \'fall\' => array(9, 10, 11),
     \'winter\' => array(12, 1, 2),
     \'spring\' => array(3, 4, 5)
   );

   foreach($$hemisphere as $key=>$val) {
     if(in_array($m, $val)) {
       $classes[] = $key;
       return $classes;
     }
   }
   $classes[] = \'\';
   return $classes;
}
css

#masthead, .site-header, .winter
{
  background-image: url(\'/wp-content/uploads/2020/09/winter.jpg\');

}
#masthead, .site-header, .spring
{
  background-image: url(\'/wp-content/uploads/2020/09/spring.jpeg\');

}
#masthead, .site-header, .summer #site-header-wrapper
{
  background-image: url(\'/wp-content/uploads/2020/09/summer.jpeg\');

}
#masthead, .site-header, .fall
{
  background-image: url(\'/wp-content/uploads/2020/09/autumn.jpg\');

}

1 个回复
SO网友:Valerii Vasyliev

更改筛选器调用

add_filter(\'masthead\',\'seasonal_site_header\', 10, 2);

function seasonal_site_header ($classes = \'\') {
   $hemisphere = \'northern\';
   $m = date(\'m\');
    
   $northern=array(
     \'summer\' => array(6, 7, 8),
     \'fall\' => array(9, 10, 11),
     \'winter\' => array(12, 1, 2),
     \'spring\' => array(3, 4, 5)
   );

   foreach($$hemisphere as $key=>$val) {
     if(in_array($m, $val)) {
       $classes[] = $key;
       return $classes;
     }
   }
   return $classes;
}
更改css

#masthead.site-header.winter
{
  background-image: url(\'/wp-content/uploads/2020/09/winter.jpg\');

}
#masthead.site-header.spring
{
  background-image: url(\'/wp-content/uploads/2020/09/spring.jpeg\');

}
#masthead.site-header.summer
{
  background-image: url(\'/wp-content/uploads/2020/09/summer.jpeg\');

}
#masthead.site-header.fall
{
  background-image: url(\'/wp-content/uploads/2020/09/autumn.jpg\');

}