简体   繁体   English

使用wp_enqueue_script的WordPress问题

[英]WordPress Problem with wp_enqueue_script

I try to us wp_enqueue_script to load my javascript, here is my code: 我试着给我们wp_enqueue_script加载我的javascript,这是我的代码:

<?php wp_enqueue_script('slider','/wp-content/themes/less/js/slider.js',array('jquery'),'1.0'); ?>

It's not working, when I look into the source, it turns to be: 它不起作用,当我查看源代码时,它变成:

<script type='text/javascript' src='http://localhost/wp/wp-content/themes/less/js/slider.js?ver=2.9.2'></script> 

?ver=2.9.2 is added to the end automatically, I guess this is the reason, how can I fix it. ?ver = 2.9.2自动添加到最后,我猜这就是原因,我该如何修复它。

Wordpress's documentation is poorly documented in this regard. 在这方面,Wordpress的文档记录很少。

Change from false to null in the second last parameter to remove ?ver=2.9.2 . 在第二个最后一个参数中从false更改为null以删除?ver=2.9.2

To remove the version parameter you need an extra filter. 要删除版本参数,您需要一个额外的过滤器。 This is how I use Google's jQuery without a query string: 这就是我在没有查询字符串的情况下使用Google的jQuery的方法:

<?php
// Use the latest jQuery version from Google
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', false, false);
wp_enqueue_script('jquery');

add_filter('script_loader_src', 'toscho_script_loader_filter');

function toscho_script_loader_filter($src)
{
    if ( FALSE === strpos($src, 'http://ajax.googleapis.com/') )
    {
        return $src;
    }
    $new_src = explode('?', $src);

    return $new_src[0];
}
?>

You may even use the last filter to add your own query vars. 您甚至可以使用最后一个过滤器来添加自己的查询变量。

Usually the query string should not affect your script. 通常,查询字符串不应该影响您的脚本。 I remove it just to increase the probability that the user can use a cached version of this file. 我删除它只是为了增加用户可以使用此文件的缓存版本的概率。

如果您使用的是Wordpress 3.0,则可以使用null作为第四个参数。这将有效地删除该版本。

将您的代码更改为:

<?php wp_enqueue_script('slider','/wp-content/themes/less/js/slider.js',array('jquery'),null); ?>

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

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