更改样式表取决于Wordpress

时间:2016-10-22 22:40:24

标签: wordpress

我需要根据一天中的时间更改样式表,例如:

if current_time > 6pm { use stylesheet 1} else { stylesheet 2 }

我使用jQuery执行此操作:

function getStylesheet() {
  var currentTime = new Date().getHours();
  if (7 >= currentTime&&currentTime > 18) {
   $('head').append("<link rel='stylesheet' href='css/day.css' type='text/css'>");
  }
  else {
   $('head').append("<link rel='stylesheet' href='css/night.css' type='text/css'>");
  }
}

getStylesheet();

我如何在Wordpress上做同样的事情?

我看看codex,我可以使用current_time,

$time = current_time( 'timestamp', 1  );

但我是如何做到的:

$night = "return 6pm here"

3 个答案:

答案 0 :(得分:2)

在注册样式表的位置尝试这样。

if ( current_time('H') >= 7 && current_time('H') < 18) {
    wp_enqueue_style( 'time-stylesheet', get_template_directory_uri() . '/css/day.css', array(), null );
} else {
    wp_enqueue_style( 'time-stylesheet', get_template_directory_uri() . '/css/night.css', array(), null );
}
  

注意: current_time()它会返回当前时间   网站/博客不是用户/访客时间。因此,请将您的博客/站点时区设置为UTC。

没有经过测试,请告诉我。发生了什么。

答案 1 :(得分:1)

您还可以使用js进行基于客户端的时间检测更合适..这样做的最佳代码来源https://css-tricks.com/snippets/javascript/different-stylesheet-pending-the-time-of-day/将该片段放在主题js文件中或直接放在主题header.php中

答案 2 :(得分:0)

您也可以使用'style_loader_src'过滤器来过滤条件,或更改返回字符串,使用所需的样式,例如:

add_filter( 'style_loader_src', function($href){
if(strpos($href, "name-of-allowed.css") !== false) {
return $href;
}
return false;
});