Javascript jQuery函数使用enqueue创建Wordpress子主题
我对wordpress的了解非常少,急需帮助向wordpress子主题添加一些jQuery函数 我基本上想添加到我的Javascript jQuery函数使用enqueue创建Wordpress子主题,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我对wordpress的了解非常少,急需帮助向wordpress子主题添加一些jQuery函数 我基本上想添加到我的首页.php 我有些了解如何使用functions.php将脚本排队。 下面是我的functions.php文件的外观 <?php function enqueue_scripts() { wp_register_script( 'js name', get_template_directory_uri() . '/myfile.js', array( 'jquer
首页.php
我有些了解如何使用functions.php
将脚本排队。
下面是我的functions.php
文件的外观
<?php
function enqueue_scripts() {
wp_register_script( 'js name', get_template_directory_uri() . '/myfile.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'js name', get_template_directory_uri() . '/myfile.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'js name', get_template_directory_uri() . '/myfile.js', array( 'jquery' ), '1.0', true );
wp_enqueue_script( 'global' );
if ( is_page( 'front-page' ) ) { // example page
wp_enqueue_script( 'js name', 'js name', 'js name' );
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_scripts' );
?>
到目前为止,它当然不起作用
非常感谢您的帮助。谢谢。试试这个:
function yourtheme_enqueue_scripts() {
wp_register_script( 'js_name', get_template_directory_uri() . '/myfile.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'js_name_2', get_template_directory_uri() . '/myfile2.js', array( 'jquery' ), '1.0', true );
wp_register_script( 'js_name_3', get_template_directory_uri() . '/myfile3.js', array( 'jquery' ), '1.0', true );
if ( is_front_page() ) {
wp_enqueue_script( 'js_name' );
wp_enqueue_script( 'js_name_2' );
wp_enqueue_script( 'js_name_3' );
}
}
add_action( 'wp_enqueue_scripts', 'yourtheme_enqueue_scripts' );
在主题根文件夹(与functions.php相同的文件夹)中,创建名为myfile.js的新文件。这是您在上述函数中引用的文件
随着主题大小的增加,最好组织文件。可以通过创建子文件夹来完成此操作,如下所示:
- 创建名为assets的文件夹(名称可以是任何内容)李>
- 在资产内部创建三个名为js、css和images的新文件夹李>
- 把你的文件放在正确的文件夹里李>
- 更新您的功能,以遵循文件夹的正确路径李>
注意:您不必关闭functions.php中的
?>
。您的js name
s中不应该有空格。另外,不知道为什么要注册同一个脚本三次……也给您的enqueue_脚本函数一个唯一的名称。我假设你的意思是脚本调用没有出现在你的源代码中?而且不仅仅是效果不起作用?对不起,我应该说得更清楚一点,我没有注册相同的脚本。我有3个脚本,它们都有不同的名称,名称中没有空格。我真正不明白的是如何在我的front-page.php中调用脚本。这是对脚本jQuery(document).ready(函数($){('.counter').counter({delay:10,time:1000});})的调用代码>谢谢!如何从front-page.php触发脚本?这是我最困惑的部分。我在哪里以及如何添加这个:jQuery(document).ready(函数($){('.counter').counter({delay:10,time:1000});})
在你的主题根文件夹中,创建一个名为myfile.js的文件,并将你的jQeury添加到其中,而不使用脚本标记。那么我是否也必须将myfile.js列在functions.php列表中?你必须同时做这两件事,请参阅我的更新答案。您的主题标题中有
之前的
吗?Tim感谢您的帮助。我仍然有这个问题。js代码应该以jQuery(document.ready
开始,还是可以直接从(函数($){
)开始?我已经将两个js文件组合成一个文件,使其对我来说更简单,下面是我的代码的开始方式。这是在wordpress之外工作的…(函数($){“使用严格的”;$.fn.counterUp=函数(选项){
我只能显示开头,因为它太长,无法在这里发布。
wp_register_script( 'js_name', get_template_directory_uri() . '/assets/js/myfile.js', array( 'jquery' ), '1.0', true );