无法在 Wordpress 子主题中加载脚本

Trouble loading scripts in Wordpress child theme

我正在尝试开发我的第一个 Wordpress 主题,但在我的子主题的 functions.php 文件中加载几个 javascript 文件时遇到问题。我一定是遗漏了一些东西,因为我的脚本不是 运行,但我相当肯定我的语法是正确的。

这是我添加到子主题 functions.php 文件中的代码。它加载了一个 javascript 动画库,然后是我的自定义脚本,none 其中依赖于 jQuery.

add_action('wp_enqueue_scripts', 'custom_theme_scripts');

function custom_theme_scripts() {
    wp_register_script('GSAP','http://cdnjs.cloudflare.com/ajax/libs/gsap/latest/TweenLite.min.js', true);
    wp_register_script('Animations', get_stylesheet_directory_uri() . '/animation.js', true);

    wp_enqueue_script('GSAP');
    wp_enqueue_script('Animations');
}

这是我针对页面上 id 为 "logo"

的 .svg 元素的自定义脚本
window.onload = function() {

    var logo = document.getElementById("logo");
    TweenLite.to(logo, 1.5, { width:5000 });

};

我将您的代码复制并粘贴到我的一个 child 主题中,包括您的 javascript 代码并且所有运行都没有问题。

确保在为自己的 WordPress 主题创建 child 主题时进行了正确的设置:

  1. style.css:检查它是否包含正确的 header,它基本上包括带有 parent 主题目录名称的倾斜行。
  2. functions.php:当您包含 child 主题时,您必须在 functions.php 的 functions.php 中添加 parent style.css 的注册31=] 主题。换句话说,在激活您的 child 主题之前,请确保在注册脚本时包含以下内容:

wp_register_style( 'childstyle', get_stylesheet_directory_uri() . '/style.css'  );
wp_enqueue_style( 'childstyle' );

此外,如果一切设置正确,它应该会加载您的脚本。通过浏览器中的开发工具检查已加载的资源。或者尝试加载任何其他 javascript 行来检查脚本是 运行.

如果主题设置的其余部分已正确完成,则您发布的脚本可以正常工作。我不能说更多,希望对您有所帮助