简体   繁体   English

使用wp_enqueue_script()在Wordpress-functions.php中添加Javascript

[英]Add Javascript in Wordpress - functions.php with wp_enqueue_script()

I want to add my custom javascript to my wordpress page. 我想将自定义javascript添加到我的wordpress页面。

I tried it usign wp_enqueue_script() but its not working. 我尝试使用wp_enqueue_script()但无法正常工作。 When I tried to add script directly in Inspect element it works properly so my script is correct. 当我尝试直接在Inspect元素中添加脚本时,它可以正常工作,因此我的脚本是正确的。

Now I create function in function.php file to add script and its not working. 现在,我在function.php文件中创建函数以添加脚本,但该脚本无法正常工作。 It shows script / file is loaded in page when I check source of page. 当我检查页面的来源时,它显示脚本/文件被加载到页面中。 But it doing nothing. 但是它什么也没做。

Below is my script and function. 以下是我的脚本和功能。

Javascript - Javascript-

jQuery(".tp-tab").mouseenter(function() {
  jQuery('.tp-tab').removeClass("selected");
});
jQuery(".tp-tab").click(function() {
  jQuery(this).addClass("selected");
});

Function in function.php function.php中的函数

    function mytheme_custom_scripts() {

            if ( !is_admin() ) {
                $scriptsrc = get_stylesheet_directory_uri() . '/wp-content/themes/circles/js/';
                wp_register_script( 'myhandle', $scriptsrc . 'slider_hover_effect.js', 'jquery', '1.0',  false );
                wp_enqueue_script( 'myhandle' );
            }

        }

add_action( 'wp_enqueue_scripts', 'mytheme_custom_scripts' );

In /wp-content/themes/circles/js/ directory location my script is palced with file name of slider_hover_effect.js which contain above javascript code. /wp-content/themes/circles/js/目录位置中,我的脚本使用slider_hover_effect.js文件名slider_hover_effect.js ,其中包含上述javascript代码。

Please let me know is there any other way I can add this script in my wordpress website. 请让我知道,还有其他方法可以在我的wordpress网站中添加此脚本。 Also I want to add/apply this script only for Homepage. 我也只想为首页添加/应用此脚本。 So is this possible. 这是可能的。

Thanks, 谢谢,

Update 更新资料

I am not sure about that I should provide complete path after get_stylesheet_directory_uri() or just /js/ folder. 我不确定在get_stylesheet_directory_uri()还是在/js/文件夹之后get_stylesheet_directory_uri()应该提供完整路径。

Or 要么

Can I use like - 我可以像这样使用吗?

function slider_effect() { ?>
    <script type="text/javascript">

jQuery(".tp-tab").mouseenter(function() {
  jQuery('.tp-tab').removeClass("selected");
});
jQuery(".tp-tab").click(function() {
  jQuery(this).addClass("selected");
});

    </script>

    <?php

}

add_action('wp_head','slider_effect');

In your functions.php 在您的functions.php中

function mytheme_custom_scripts(){
    if ( is_home() || is_front_page()) {
            $scriptSrc = get_template_directory_uri() . '/js/slider_hover_effect.js';
            wp_enqueue_script( 'myhandle', $scriptSrc , array(), '1.0',  false );
    }
}
add_action( 'wp_enqueue_scripts', 'mytheme_custom_scripts' );

JavaScript file (slider_hover_effect.js) JavaScript文件(slider_hover_effect.js)

$( document ).ready(function($) {
    $( 'body' ).on( 'mouseenter', '.tp-tab', function () {
        $(this).removeClass("selected");
    });
    $( 'body' ).on( 'click', '.tp-tab', function () {
        $(this).addClass("selected");
    });
});
  1. Check if there is any error in browser console. 检查浏览器控制台中是否有任何错误。
  2. View page source and see if the file is included at the bottom of source. 查看页面源,并查看文件是否包含在源底部。
  3. There might be some precedence issue, to check that try to change 'false' parameter to 'true' in wp_enqueue_script function (so the script will be added to header). 可能存在一些优先级问题,以检查是否尝试在wp_enqueue_script函数中将“ false”参数更改为“ true”(因此脚本将被添加到标头)。

I hope this helps. 我希望这有帮助。

Try this: 尝试这个:

functions.php functions.php

function mytheme_custom_scripts() {
// No need to check admin, 'wp_enqueue_scripts' only enqueues at the frontend 
            $scriptsrc = get_stylesheet_directory_uri() . '/js/';
            wp_register_script( 'myhandle', $scriptsrc . 'slider_hover_effect.js', array('jquery'), '1.0',  false );
            wp_enqueue_script( 'myhandle' );

    }

add_action( 'wp_enqueue_scripts', 'mytheme_custom_scripts' );

Check full codex: https://developer.wordpress.org/reference/functions/wp_enqueue_script 检查完整的编解码器: https : //developer.wordpress.org/reference/functions/wp_enqueue_script

You should not print JS in 'wp_head' unless you can't do otherwise. 除非您不能这样做,否则不应在“ wp_head”中打印JS。

slider_hover_effect.js slide_hover_effect.js

jQuery(document).ready(function($){
   $(".tp-tab").mouseenter(function() {
     $(this).removeClass("selected");
   });
   $(".tp-tab").click(function() {
     $(this).addClass("selected");
   });
});

Try this ,change !is_admin() with is_home() 试试这个,用is_home()改变!is_admin() is_home()

 function mytheme_custom_scripts() {
          if ( is_home() ) {
                    $scriptsrc = get_stylesheet_directory_uri() . '/js/';
                    wp_enqueue_script( 'myhandle', $scriptsrc . 'slider_hover_effect.js', array(), '1.0',  false );
           }
     }

add_action( 'wp_enqueue_scripts', 'mytheme_custom_scripts' );

or: 要么:

function slider_effect() {
if ( is_home() ) {
 ?>
    <script type="text/javascript">
jQuery(function(){
jQuery(".tp-tab").mouseenter(function() {
  jQuery('.tp-tab').removeClass("selected");
});
jQuery(".tp-tab").click(function() {
  jQuery(this).addClass("selected");
});
});
    </script>

    <?php }

}

add_action('wp_head','slider_effect');

I believe you forgot the src parameter in the wp_enqueue_script when you called it in your callback mytheme_custom_scripts 我相信您在回调mytheme_custom_scripts调用wp_enqueue_script时忘记了src参数

From wordpress docs: 从wordpress文档中:

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

Parameters 参量

$handle (string) (Required) Name of the script. $ handle(字符串)(必需)脚本名称。

$src (string|bool) (Optional) Path to the script from the root directory of WordPress. $ src(string | bool)(可选)来自WordPress根目录的脚本路径。 Example: '/js/myscript.js'. 示例:“ / js / myscript.js”。 Default value: false 默认值:false

So I believe your code should be: 因此,我相信您的代码应为:

 wp_enqueue_script( 'myhandle' , $scriptsrc . 'slider_hover_effect.js');

Another way is if you can create your custom plugin and activate that plugin to allow you to include custom js or css like this in your index.php file of the plugin: 另一种方法是,如果您可以创建自定义插件并激活该插件,以允许您在插件的index.php文件中包含这样的自定义js或CSS:

add_action( 'init', 'my_script' );

function my_script() {
    if( is_home() ){
        wp_enqueue_script('my_script', plugins_url('js/script.js', __FILE__), array('jquery'));
        wp_enqueue_style('my_style', plugins_url('css/style.css', __FILE__));
    }
}

You place the files in the js/ or css/ dir and this works 您将文件放在js /或css /目录中

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

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