【问题标题】:Using a custom JQuery script in WordPress without updating the theme functions.php在 WordPress 中使用自定义 JQuery 脚本而不更新主题 functions.php
【发布时间】:2023-03-13 14:13:01
【问题描述】:

我已将自定义 JQuery 脚本添加到新的 JS 文件“themes\twentytwentyone\js\script_new.js”中。

我将这个新的 JS 文件加载到主题 functions.php 文件中。

function add_custom_script() {
    wp_enqueue_script( 
        'my_script', 
        get_stylesheet_directory_uri() .'/js/script_new.js', 
        array( 'jquery' ), 
        '1.0', 
        true
    );
} 
add_action( 'wp_enqueue_scripts', 'add_custom_script' ); 

这没有任何问题。但是有没有办法实现这个不更新主题functions.php文件? 我不确定更新 functions.php 文件是否是一种好习惯。

【问题讨论】:

  • 考虑为您的案例使用子主题。
  • 使用名为code snippet的插件,它可以让你写一个PHP sn-ps

标签: php jquery wordpress custom-wordpress-pages


【解决方案1】:

最好使用儿童主题functions.php。

但是如果你不想使用functions.php,那么你也可以通过footer.php或header.php调用js文件。

请注意我建议,为任何自定义制作子主题并使用子主题的functions.php是最安全的。

【讨论】:

    猜你喜欢
    • 2018-10-20
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    相关资源
    最近更新 更多