简体   繁体   English

在WordPress中包含javascript

[英]Including javascript in wordpress

I want to include my own javascript code in the front page of a wordpress site. 我想在wordpress网站的首页中包含自己的javascript代码。 I have located it and have tried many ways online to add javascript code to it but either the console says it failed to load the resource or that the method I called is not defined when I try to use it in an "onload" attribute. 我已经找到了它,并尝试了许多在线方法来向其中添加javascript代码,但是控制台说它无法加载资源,或者当我尝试在“ onload”属性中使用它时,我定义的方法没有定义。 I have looked for many articles online but they seem to be talking about how to use it in a specific post. 我在网上寻找了很多文章,但他们似乎在谈论如何在特定帖子中使用它。 I want this code for the front page only. 我只想将此代码用于首页。

This is how I have included the javascript most recently in the header file: 这就是我最近在头文件中包含javascript的方式:

<?php echo '<script type="text/javascript"';
echo 'src="virtue-child/aboutUs.js"></script>'; ?>

You can create custom script file in js folder of your theme and call in your theme function.php 您可以在主题的js文件夹中创建自定义脚本文件,然后调用主题function.php

Ex: your script file name ->customjquery.js 例如:您的脚本文件名-> customjquery.js

function custom_scripts() {

    wp_enqueue_script( 'customjquery', get_template_directory_uri() . '/js/customjquery.js', array(), '0001', true );

}
add_action( 'wp_head', 'custom_scripts' );

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

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