【问题标题】:Wordpress function.php wp_enqueue_script doesn't workWordpress function.php wp_enqueue_script 不起作用
【发布时间】:2021-12-01 18:25:57
【问题描述】:

我正在尝试将 jquery 集成到我的 wordpress 主题中。我已将此代码集成到我的 function.php 中,但它不起作用。任何人都可以看到问题吗?谢谢。

//Enqueue Child Styles & Scripts on the Front-End
add_action('wp_enqueue_scripts', function(){
    wp_enqueue_style('nebula-child'); //Stylesheets
    wp_enqueue_script('nebula-main'); //Scripts
    wp_enqueue_script( 'custom', get_stylesheet_directory_uri() . '/assets/js/custom.js', array ( 'jquery', '3.3.2'), '', true);
}, 327);

我的 custom.js 文件:

jQuery(document).ready(function($){
    console.log( "ready!" );
});

【问题讨论】:

  • 你可以点击开发者工具的网络标签,检查是否加载了custom.js
  • 很遗憾没有加载
  • 您的 custom.js 文件是否有任何 404 错误未找到?您确定文件在此路径中的位置正确吗?此外,您能否给我们一个指向您的域 - 站点的链接?
  • 我在控制台中没有与此问题相关的错误,也没有 404 错误。正如霍华德所说,不在我的 wp_enqueue_ 脚本中指定“jquery”就足够了

标签: jquery wordpress


【解决方案1】:

您的 Enqueue 是错误的。您的 enqueue 脚本说名为 3.3.2 的脚本是一个依赖项,除非它通过该句柄找到脚本,否则不会加载您的脚本。

wp_enqueue_script( 'custom', get_stylesheet_directory_uri() . '/assets/js/custom.js', array ( 'jquery'), '3.3.2', true);

3.3.2 将是您的脚本版本,如果您打算使用 jQuery 版本...不要。

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

【讨论】:

  • 感谢霍华德,您建议的解决方案似乎有效。我只是在我的 wp_enqueue_script 中删除了“array ('jquery', '3.3.2')”
  • 你想要array('jquery') 只需记下其他值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2011-05-29
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
相关资源
最近更新 更多