【问题标题】:Adding Custom Script in WordPress Child Theme在 WordPress 子主题中添加自定义脚本
【发布时间】:2014-04-02 19:14:31
【问题描述】:

我想在 WordPress 中包含一个用于子主题的自定义 jQuery 脚本。我在我的 WP Child Theme 文件夹/目录中的 function.php 文件中使用下面的脚本。

问题是脚本将它附加到 url 中的父主题。例如它正在输出

localhost:8888/wordpress/wp-content/themes/responsive/custom_js/footer.js

应该是什么时候

localhost:8888/wordpress/wp-content/themes/responsive-childtheme-master/custom_js/footer.js

<?
function jerrell_adds_to_the_footer(){

    wp_register_script('add-footer-js', get_template_directory_uri() . '/custom_js/jquery_test.js', array('jquery'),'', true );

    wp_enqueue_script('add-footer-js');

}

    add_action('wp_enqueue_scripts', 'jerrell_adds_to_the_footer'); //Hooks my custom function into WP's wp_enqueue_scripts function
    ?>

【问题讨论】:

    标签: jquery wordpress wordpress-theming


    【解决方案1】:

    get_stylesheet_directory_uri() 应该返回子主题的目录,而get_template_directory_uri() 返回父主题的目录。

    【讨论】:

      【解决方案2】:

      由于您使用的是子主题,因此您应该使用get_stylesheet_directory_uri() 而不是get_template_directory_uri()

      <?
      function jerrell_adds_to_the_footer(){
      
      wp_register_script('add-footer-js', get_stylesheet_directory_uri() . '/custom_js/jquery_test.js', array('jquery'),'', true );
      
      wp_enqueue_script('add-footer-js');
      
      }
      
      add_action('wp_enqueue_scripts', 'jerrell_adds_to_the_footer'); //Hooks my custom     function into WP's wp_enqueue_scripts function
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        相关资源
        最近更新 更多