【问题标题】:WordPress wp_enque_scripts doesn't seem to workWordPress wp_enque_scripts 似乎不起作用
【发布时间】:2017-11-25 06:22:40
【问题描述】:

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

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

调用失败

video_bg()

这是我孩子的functions.php:

<?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;

这是控制台告诉我的:

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

谁能告诉我为什么 video_bg() 永远不会被调用?还是有其他问题?

【问题讨论】:

    标签: javascript php wordpress


    【解决方案1】:

    如果在添加某个 add_action 时未调用该函数,则可能有两种情况: 1. 您输入了错误的操作/过滤器挂钩名称。 2. 你加载的页面不会触发那个钩子。

    在这种情况下,你做了第一个。没有一个叫做 wp_enque_scripts 的钩子,没有一个叫做 wp_enque_scripts 的函数。 将它们更改为 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;
    

    你也可以这样优化你的代码:

    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>';
        }
    

    【讨论】:

    • 哇,好尴尬,哈哈。谢谢!但是,现在WordPress似乎完全忽略了孩子的functions.php。没有任何“echo”语句打印到控制台。即使我将代码减少到 &lt;?php tagecho 行 - 它仍然没有显示。可能与代码无关 - 也许 WordPress 只是在窃听。我应该检查什么?谢谢一百万!
    • 可能是在此之前在父主题中声明了 post_theme_setup,这就是原因。尝试在子主题中重命名该功能。实际上你不需要它。
    • 其实还是有问题的。回声只是一个 Firefox 控制台问题 - 在 Chrome 中有效。 wp_enqueue_scripts( 'myVideo', get_stylesheet_directory_uri() . '/JS/filmScript.js', array('jquery'), '1.0.0', false ); 行虽然会导致内部服务器错误。不知道为什么。当我将其注释掉时 - 一切都没有问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2016-02-01
    • 2020-09-23
    • 2010-12-05
    • 2011-06-14
    • 2015-01-10
    • 2016-02-24
    相关资源
    最近更新 更多