未显示注册脚本版本

时间:2016-08-24 作者:Pierre

我想在特定脚本上实现缓存破坏,但该版本并没有作为查询变量添加到url中。

下面是我使用的代码:

wp_register_script( \'custom\', get_stylesheet_directory_uri() . \'/js/custom.min.js\', array(\'jquery\',\'modernizr\'), filemtime( get_stylesheet_directory().\'/js/custom.min.js\' ), true );
我正在使用filemtime函数获取上次修改的时间戳作为我的版本。

但从消息来源来看:

<script type=\'text/javascript\' src=\'http://example.com/app/themes/MyTheme/js/custom.min.js\'></script>
我想要的是http://example.com/app/themes/MyTheme/js/custom.min.js?v=1472031892

1 个回复
SO网友:jgraup

我可以验证这是否适用于我,它看起来很像您提供的代码。

也许您正在其他地方定义脚本。你能给它一个新的句柄名称来检查吗(\'custom-xyz\')?

此外,您是否可以测试filemtime 看看它输出了什么?我添加了一个“v“在它前面以防它回来false.

$ver = "v" . filemtime( get_stylesheet_directory().\'/js/custom.min.js\' );

wp_register_script( \'custom\',

    get_stylesheet_directory_uri() . \'/js/custom.min.js\',

    array( \'jquery\',\'modernizr\' ),

    $ver,

    true);

wp_enqueue_script( \'custom\' );
如果由于奇怪的原因,您在注册时无法使其正常工作,您可以使用script_loader_tag 还有一些正则表达式或str_replace.

wp_register_script( \'custom\',

    get_stylesheet_directory_uri() . \'/js/custom.min.js\',

    array( \'jquery\',\'modernizr\' ),

    \'%%CACHE_BUSTER%%\',

    true);

wp_enqueue_script( \'custom\' );

function script_tag_cache_buster( $tag, $handle, $src ) {

    if ( \'custom\' === $handle ) {
        return str_replace( \'%%CACHE_BUSTER%%\', "v" . filemtime( get_stylesheet_directory().\'/js/custom.min.js\'), $tag );
    }

    return $tag;
}

add_filter( \'script_loader_tag\', \'script_tag_cache_buster\', 10, 3 );

相关推荐

Wp_cache_set不工作有什么原因吗?

wp\\u cache\\u设置不工作的原因是什么?我一直在转动车轮,试图弄明白为什么这些都不起作用。有什么建议吗?这些函数旨在帮助我将结果缓存到键/对象,然后利用键/对象显示信息。但是,它们不存储密钥/对象$related_post_ids = wp_cache_get( \'related_post_ids\' ); if ( false === $related_post_ids ) { //seting args and run query $the_query = new