繁体   English   中英

为什么home_url()在WordPress中不能与filemtime()一起使用?

[英]Why does home_url() not work with filemtime() in WordPress?

我想开始自动更改入队版本,因此在文件编辑后不必手动更改它,因此我考虑过使用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' );

有用。 我研究并阅读:

但是我没有找到为什么filemtime()home_url可以在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'; $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' />

但Bootstrap文件未呈现。

我认为原因是PHP文件系统功能不是基于URL,而是指向文件的绝对路径。 因此,我们需要该文件的URL和绝对路径,以便我们可以确保该文件存在并获取文件时间戳:

function bootstrap_enqueue() {

    // Roots
    // $bootstrap_abs   = ASBSPATH  . '/css/bootstrap.css';
    // $bootstrap_url   = home_url() . '/css/bootstrap.css';

    $bootstrap_abs  = get_stylesheet_directory() . '/css/bootstrap.css';
    $bootstrap_url  = get_stylesheet_directory_uri() . '/css/bootstrap.css';

    if( file_exists( $bootstrap_abs ) ) :
        $bootstrap_ver = date( "y.m.d", filemtime( $bootstrap_abs ) );
    else :
        $bootstrap_ver = '1.0.0';
    endif;

    wp_enqueue_style('bootstrap-style', $bootstrap_url, array(), $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');

“ home_url”功能会回显家庭URL,这将破坏您的代码。 请改用get_home_url() ,因为该函数返回该值,这意味着您可以将其存储在变量中或回显它。

wp_enqueue_style('bootstrap-style', get_home_url() . '/css/bootstrap.css', '1.0' );

EDITED

PHP filemtime()函数需要从服务器根目录获取文件URL,但是wordpress wp_enqueue_style()函数需要主机根URL。 只要您的CSS仍在根目录中,对代码的以下更改就可能起作用。 如果这是您的主题目录,只需将get_home_url()更改为get_template_directory_uri() 如果那不起作用,那就给我抽个腌鱼,我会回去吃早餐。

function bootstrap_enqueue() {
    $bootstrap_file = get_home_url() . '/css/bootstrap.css';
    if (file_exists($bootstrap_file)) :
        $bootstrap_ver = date("y.m.d", filemtime($_SERVER["DOCUMENT_ROOT"] . '/css/bootstrap.css'));
    else :
        $bootstrap_ver = '1.0.0';
    endif;
    wp_enqueue_style('bootstrap-style', $bootstrap_file, $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');

关闭-我认为问题是filemtime需要文件路径资产,而您正在尝试将其提供给Web URL。

我会尝试类似使用getcwd()指向文件的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM