我想在10个不同的javascript文件中使用一个函数(每个都在不同的网页上使用)。我希望避免将函数复制/粘贴到每个.js文件中。
这些页面都有相同的父页面。
有什么方法可以定义这个函数并从每个页面的.js文件中调用它吗?
提前谢谢你。
发布于 2019-08-19 14:36:55
只需在它自己的单独JS文件中定义函数,并在页面特定的文件旁边加载该文件。"WordPress方法“是注册包含共享函数的脚本(S),然后将其设置为其他脚本的依赖项:
wp_register_script( 'my-functions', get_theme_file_uri( 'js/functions.js' ) );
if ( is_page( 1 ) ) {
wp_enqueue_script( 'my-page-1', get_theme_file_uri( 'js/page-1.js' ), [ 'my-functions' ] );
}
if ( is_page( 2 ) ) {
wp_enqueue_script( 'my-page-2', get_theme_file_uri( 'js/page-2.js' ), [ 'my-functions' ] );
}
// etc.https://wordpress.stackexchange.com/questions/345424
复制相似问题