繁体   English   中英

将Wordpress插件脚本排在所有其他JS下面

[英]Enqueue Wordpress plugin scripts below all other JS

我正在开发一个简单的Wordpress应用程序,但我遇到了一个问题,因为所有插件脚本都是在我的functions.php中排队的那些之前呈现的。

以下是functions.php的示例部分:

function my_scripts() {
    wp_register_script('app-js', get_template_directory_uri() . '/javascripts/app.js', array('jquery'), null, true );
    wp_enqueue_script('app-js');
}
add_action( 'wp_enqueue_scripts', 'my_scripts');

需要注意的重要一点是(按照最佳实践,我的JS设置为在页面底部呈现。

我也有几个插件在主题上运行。 问题是输出看起来像这样:

<!-- Note the plugin appears first -->
<script type='text/javascript' src='http://localhost/wordpress/wp-content/plugins/my-plugin/acl-plugin.js'></script>
<!-- And the main JS appears second -->
<script type='text/javascript' src='http://localhost/wordpress/wp-content/themes/my-theme/javascripts/app.js'></script>
</body>
</html>

如何强制Wordpress显示主JS(我相信wp_head()呈现在主页的最底部?

WordPress的wp_head()方法将只显示有在WordPress的wp_enqueue_script()设置为false,最后一个参数。当它被设置为true,将在页脚通过wp_footer渲染输出脚本或样式()

您可以通过调整add_action()中$ priority参数来更改其调用和插入时的优先级

http://codex.wordpress.org/Function_Reference/add_action

$ priority(int)(可选)用于指定与特定操作关联的函数的执行顺序。 较低的数字与先前的执行相对应,具有相同优先级的函数按照它们添加到操作的顺序执行。 默认值:10

add_action( $hook, $function_to_add, $priority, $accepted_args );

并且还看看以下两个WordPress方法:

wp_enqueue_script()

https://developer.wordpress.org/reference/functions/wp_enqueue_script/

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

wp_register_script()

https://developer.wordpress.org/reference/functions/wp_register_script/

wp_register_script( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

尝试这个..

您可能必须使用该$ priority参数

function my_scripts() {
        wp_register_script('app-js', get_template_directory_uri() . '/javascripts/app.js', array('jquery'), null, true );
        wp_enqueue_script('app-js');
}
add_action( 'wp_enqueue_scripts', 'my_scripts', 20, 1);

暂无
暂无

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

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