主题开发:从functions.php链接时JavaScript不工作
我使用html、css和javascript创建了一个包含两个模态的标题。将这些链接到html文档中 我已经决定创建一个wordpress主题,并将包含提到的标题。我遵循Wordpress的Theme Developer手册,简单地将代码复制并粘贴到header.php中需要的地方 因为这样做更合适,所以我创建了一个functions.php文件,并使用wp_enqueue_脚本添加我的javascript。它不起作用。当我说不工作时,我的意思是它甚至没有加载到页面上 functions.php主题开发:从functions.php链接时JavaScript不工作,javascript,php,wordpress,Javascript,Php,Wordpress,我使用html、css和javascript创建了一个包含两个模态的标题。将这些链接到html文档中 我已经决定创建一个wordpress主题,并将包含提到的标题。我遵循Wordpress的Theme Developer手册,简单地将代码复制并粘贴到header.php中需要的地方 因为这样做更合适,所以我创建了一个functions.php文件,并使用wp_enqueue_脚本添加我的javascript。它不起作用。当我说不工作时,我的意思是它甚至没有加载到页面上 functions.php
<?php
function k9stormdomestictheme_script_enqueue() {
wp_enqueue_style( 'k9stormdomesticstyle', get_stylesheet_uri() );
wp_enqueue_script( 'k9stormdomesticscript', get_template_directory_uri() . '/js/script.js', array (), 1.1, true);
}
add_action('wp_enqueue_scripts', 'k9stormdomestictheme_script_enqueue');
?>
<?php
get_header();
get_footer();
?>
我真的很感谢你的帮助。多谢各位 实际上,在functions.php文件中根本没有将任何外部脚本排队。您需要引用文件的路径作为
wp\u enqueue\u脚本
方法的第二个参数
get\u template\u directory\u uri()
只告诉该方法转到主题文件夹的根目录,而不指定要排队的javascript文件。它应该更像这样:
<?php
function k9stormdomestictheme_script_enqueue() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_script( 'script', get_template_directory_uri() . '*the-remaining-path-to-your-js-file*' );
}
add_action('wp_enqueue_scripts', 'k9stormdomestictheme_script_enqueue');
?>
试着提供更多的信息,说明哪些不起作用。有什么错误吗?它不是按照你期望的方式工作吗?如果是这样的话,请解释你的期望和实际发生的事情。谢谢你的投入,这对我来说很有意义。我已经做了这些更改,但仍然不起作用!一些澄清问题:(1)您是否收到错误,或者罚款只是没有排队?(2) 这是父主题还是子主题?(3) 我假设您的functions.php文件位于主题文件夹的根目录中?(1)没有错误,脚本没有排队。如果我把脚本放在header.php(2)Child-theme(3)functions.php在我的主题文件夹的根目录中,它就可以工作。
<?php
function k9stormdomestictheme_script_enqueue() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
wp_enqueue_script( 'script', get_template_directory_uri() . '*the-remaining-path-to-your-js-file*' );
}
add_action('wp_enqueue_scripts', 'k9stormdomestictheme_script_enqueue');
?>
<?php
function k9stormdomestictheme_script_enqueue() {
wp_enqueue_style( 'k9stormdomesticstyle', get_stylesheet_uri() );
wp_enqueue_script( 'k9stormdomesticscript', get_stylesheet_uri() . '/js/script.js', array (), 1.1, true);
}
add_action('wp_enqueue_scripts', 'k9stormdomestictheme_script_enqueue');
?>