简体   繁体   English

在Divi / WordPress中使用JavaScript?

[英]Using JavaScript in Divi/WordPress?

I have a small piece of JavaScript code that I want to put into my site made with the Divi theme. 我想将一小段JavaScript代码放入以Divi主题制作的网站中。 I made a child theme with a functions.php file and a js file. 我用functions.php文件和js文件制作了一个子主题。 I put the following code in the functions.php file: 我将以下代码放在functions.php文件中:

<?php
   function wpdivi_theme_name_scripts() {
  wp_enqueue_script( 'script-name', get_template_directory_uri() . 
  '/js/code.js', array(), '1.0.0', true );
  }
add_action( 'wp_enqueue_scripts', 'wpdivi_theme_name_scripts' );
?>

In divi I used the code module to set up the HTML. 在divi中,我使用了代码模块来设置HTML。 I tested the code in this fiddle . 我在这个小提琴中测试了代码。

However when I use the same code on my WordPress site it doesn't work. 但是,当我在WordPress网站上使用相同的代码时,它将无法正常工作。 I tried a simple alert box in js but that doesn't work either. 我在js中尝试了一个简单的警报框,但也不起作用。

Can someone help me? 有人能帮我吗?

This is working for our wordpress site which is used DIVI theme. 这适用于我们使用DIVI主题的wordpress网站。 Could you please check with js file path. 您能否检查js文件路径。

If you are using "get_template_directory_uri()" function, the file should be there in parent theme js folder. 如果使用“ get_template_directory_uri()”函数,则该文件应位于父主题js文件夹中。

If you are using "get_stylesheet_directory_uri()" function, the file should be there in child theme js folder. 如果使用“ get_stylesheet_directory_uri()”函数,则该文件应位于子主题js文件夹中。

Try using the code module to enter the JavaScript directly. 尝试使用代码模块直接输入JavaScript。 If you just surround your JS code in <script> tags inside the code module, it should work. 如果您只是将JS代码放在代码模块内的<script>标记中,则它应该可以工作。

或者,只需转到Divi设置>集成>添加脚本。

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

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