简体   繁体   English

在Wordpress子主题中加载脚本时遇到问题

[英]Trouble loading scripts in Wordpress child theme

I am trying to develop my first Wordpress Theme and I'm having trouble loading a couple of javascript files inside my child theme's functions.php file. 我正在尝试开发我的第一个Wordpress主题,但在我的子主题的functions.php文件中加载几个javascript文件时遇到问题。 I must be missing something as my script isn't running but I'm fairly positive my syntax is correct. 我必须丢失某些内容,因为我的脚本未运行,但是我相当肯定我的语法是正确的。

Here is the code I have added to my functions.php file in my child theme. 这是我在子主题中添加到我的functions.php文件中的代码。 It loads a javascript animation library and then my custom script, none of which rely on jQuery. 它先加载一个javascript动画库,然后加载我的自定义脚本,这些脚本都不依赖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');
}

This is my custom script targeting a .svg element on the page with an id of "logo" 这是我的自定义脚本,目标是页面上ID为“ logo”的.svg元素

window.onload = function() {

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

};

I copied and pasted your code in one of my child themes, including your javascript code and all runs with no problem. 我将您的代码复制并粘贴到了我的一个子主题中,包括您的javascript代码,所有运行都没有问题。

Be sure when you create child themes for your own WordPress themes that you make the correct setup: 确保在为自己的WordPress主题创建子主题时进行正确的设置:

  1. style.css: Check it contains the correct header, which basically includes the tilte line with the directory name of the parent theme. style.css:检查其是否包含正确的标题,该标题主要包括带有父主题目录名称的斜线。
  2. functions.php: When you include your child theme you have to add the registration of the parent style.css in the functions.php of the child theme. functions.php:当包含子主题时,必须在子主题的functions.php中添加父style.css的注册。 In other words, before activating your child theme, be sure you include this when you register scripts: 换句话说,在激活子主题之前,请确保在注册脚本时包括以下内容:

.

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

Beside that if everything is setup properly it should load your scripts. 除此之外,如果一切都正确设置,它应该加载您的脚本。 Check the resources that have been loaded through the development tools you have in your browser. 检查通过浏览器中的开发工具加载的资源。 Or try to load any other javascript line to check the script is running. 或者尝试加载其他任何JavaScript行以检查脚本是否正在运行。

That script you posted works if the rest of the theme setup has been done properly. 如果其余主题设置正确完成,则您发布的脚本将起作用。 I cannot tell more than that, I hope it helps 我不能说的更多,希望对您有所帮助

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

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