dongli1887
2017-11-25 06:22
浏览 126
已采纳

WordPress wp_enque_scripts似乎不起作用

I've created a child of a WordPress theme and now I'm trying to insert custom JavaScript into the home page by modifying functions.php of the child. However, my script is not being loaded and I'm not sure why. I've inserted 'echo' statements into the php code and it seems like

add_action( 'wp_enque_scripts', 'video_bg', 10);

fails to call

video_bg()

Here's the functions.php of my child:

<?php


add_action( 'after_setup_theme', 'post_theme_setup' );

if ( !function_exists( 'post_theme_setup' )):
function post_theme_setup(){

    function video_bg() {
        wp_enque_script( 'myVideo', get_stylesheet_directory_uri() . '/JS/filmScript.js', array('jquery'), '1.0.0', false );
        echo '<script>console.log("Script added?")</script>';
    }

    add_action( 'wp_enque_scripts', 'video_bg', 10);
    echo '<script>console.log("Loop Entered")</script>';

}
endif;

And here's what the console is telling me:

Loop Entered                                      (index):1 
JQMIGRATE: Migrate is installed, version 1.4.1    jquery-migrate.min.js?ver=1.4.1&nocache=1:2 

Could anyone please tell me why video_bg() never gets called? Or is the problem in something else?

图片转代码服务由CSDN问答提供 功能建议

我创建了一个WordPress主题的子项,现在我正在尝试将自定义JavaScript插入主页 通过修改孩子的functions.php。 但是,我的脚本没有被加载,我不知道为什么。 我在php代码中插入了'echo'语句,好像是

  add_action('wp_enque_scripts','video_bg',10); 
  <  / pre> 
 
 

无法调用

  video_bg()
   
 
 

这是函数 我孩子的.php:

 &lt;?php 
 
 
add_action('after_setup_theme','post_theme_setup'); 
 
if(!function_exists('post_theme_setup)  ')):
 
函数post_theme_setup(){
 
函数video_bg(){
 
wp_enque_script('myVideo',get_stylesheet_directory_uri()。'/ JS / filmScript.js',array('jquery'),'1.0。  0',false); 
 echo'&lt; script&gt; console.log(“脚本已添加?”)&lt; / script&gt;'; 
} 
 
 add_action('wp_enque_scripts','video_bg',10)  ; 
 echo'&lt; script&gt; console.log(“Loop Entered”)&lt; / script&gt;'; 
 
} 
endif; 
   
 
 

以下是控制台告诉我的内容:

  Loop Entered(index):1 
JQMIGRATE:已安装Migrate,版本1.4.1 jque  ry-migrate.min.js?ver = 1.4.1&amp; nocache = 1:2 
   
 
 

有人可以告诉我为什么video_bg()永远不会被调用吗? 或者其他问题是什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douyou9923 2017-11-25 07:18
    最佳回答

    If the function isn't called when you added it some add_action, there are 2 possible cases: 1. You entered wrong action/filter hook name. 2. Your loaded page doesn't trigger that hook.

    In this case you did the 1st. There is not a hook called: wp_enque_scripts, there is not a function called wp_enque_scripts. Change them to wp_enqueue_scripts.

    add_action( 'wp_enqueue_scripts', 'video_bg', 10);
    add_action( 'after_setup_theme', 'post_theme_setup' );
    
    if ( !function_exists( 'post_theme_setup' )):
    function post_theme_setup(){
    
        function video_bg() {
            wp_enqueue_scripts( 'myVideo', get_stylesheet_directory_uri() . '/JS/filmScript.js', array('jquery'), '1.0.0', false );
            echo '<script>console.log("Script added?")</script>';
        }
    
        add_action( 'wp_enqueue_scripts', 'video_bg', 10);
        echo '<script>console.log("Loop Entered")</script>';
    
    }
    endif;
    

    You can also optimize your code like that:

    add_action( 'after_setup_theme', 'post_theme_setup' );
    
    if ( !function_exists( 'post_theme_setup' )):
    function post_theme_setup(){
        add_action( 'wp_enqueue_scripts', 'video_bg', 10);
        echo '<script>console.log("Loop Entered")</script>';
    
    }
    endif;
    
      function video_bg() {
            wp_enqueue_scripts( 'myVideo', get_stylesheet_directory_uri() . '/JS/filmScript.js', array('jquery'), '1.0.0', false );
            echo '<script>console.log("Script added?")</script>';
        }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题