个性化阅读
专注于IT技术分析

在WordPress子主题中添加自定义脚本

我想在WordPress中为子主题添加自定义jQuery脚本。我正在WP Child Theme文件夹/目录下的function.php文件中使用以下脚本。

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

本地主机:8888 / wordpress / wp-content / themes / response / custom_js / footer.js

什么时候应该

本地主机:8888 / wordpress / wp-content / themes / response-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
    ?>

#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
?>
赞(0)
未经允许不得转载:srcmini » 在WordPress子主题中添加自定义脚本

评论 抢沙发

评论前必须登录!