我应该在哪里调用 wp_register_script() 在 wordpress 中加载 js?



这篇文章非常清楚地解释了如何在wordpress中加载javascript文件,期望一件事。我在哪里称呼 wp_register_script((?这么多php文件,我应该放在哪里? 我应该把我的 js 文件放在哪里? 我可以只在文本文件中使用脚本标签吗?

我假设您正在尝试在主题上添加javascript文件,并且还假设您要添加一个名为custom-script.js的javascript文件。在这种情况下,您可以在主题文件夹中创建一个名为js的文件夹,并在该文件夹中添加js文件。然后,在主题的函数.php文件中添加以下代码。

function wptuts_scripts_basic()
{    
// Register the script like this for a theme:
wp_register_script( 'custom-script', get_template_directory_uri() . '/js/custom-script.js' ); 
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_basic' );

最新更新