【发布时间】:2017-09-22 21:04:30
【问题描述】:
我想开始自动更改我的队列版本,因此我不必在文件编辑后手动更改它,所以我考虑使用 filemtime() 来拉时间,但由于某种原因,当我将它与 site_url() 一起使用时或home_url 它不起作用:
function bootstrap_enqueue() {
$bootstrap_file = home_url() . '/css/bootstrap.css';
if (file_exists($bootstrap_file)) :
$bootstrap_ver = date("y.m.d", filemtime($bootstrap_file));
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_file, $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');
但是当我通过时:
wp_enqueue_style('bootstrap-style', home_url() . '/css/bootstrap.css', '1.0' );
它有效。我研究并阅读了:
- Alternative to WordPress's get_site_url()
- Get the last modified date of a remote file
- Get last modified file in a directory
- filemtime
但我还没有找到答案为什么 filemtime() 它在 WordPress 中与 home_url 一起工作?
编辑:
我尝试过的进一步测试:
wp_enqueue_style('bootstrap-style', $bootstrap_file, array(), $bootstrap_ver);
认为这可能是一个排序问题,但仍然不起作用。我已将 CSS 文件移至主题目录并尝试:
$bootstrap_file = get_template_directory_uri() . '/css/bootstrap.css';
if (file_exists($bootstrap_file)) :
$bootstrap_ver = date("y.m.d", filemtime($bootstrap_file));
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_file, array(), $bootstrap_ver);
所有这些仍然产生相同的结果,并且版本被推送到1.0.0,所以我认为它与$bootstrap_file = home_url() . '/css/boostrap.css';有关,只是不确定是什么。
在我的脑海中返回了似乎正确的内容:
<link rel='stylesheet' id='bootstrap-style-css' href='http://path/to/server/css/bootstrap.css?ver=1.0.0' type='text/css' media='all' />
但引导文件未呈现。
【问题讨论】:
-
当您在浏览器中使用
http://path/to/server/css/boostrap.css?ver=1.0.0作为 URL 时,它会显示 css 文件吗?您是否尝试过只使用'/css/boostrap.css',因为/会进入网站的根目录。 -
是的文件呈现,但由于某种原因它没有拉版本日期。
-
试试这个
bootstrap_file = $_SERVER["DOCUMENT_ROOT"] . '/css/boostrap.css';(这个例子的CSS需要回到根目录)如果这不起作用尝试打开PHP错误报告。它可能会显示一些有用的错误。
标签: php wordpress twitter-bootstrap-3 filemtime