[英]Adding Custom Js to Wordpress Theme
I have this short javascript I want to add to a client's wordpress.我有这个简短的 javascript,我想添加到客户的 wordpress 中。 Im not really savvy on how wordpress operates or where to add my code.
我不太了解 wordpress 的操作方式或在哪里添加我的代码。 I've read some articles online but kinda confused on how to go about this.
我在网上阅读了一些文章,但对如何解决这个问题有点困惑。 I added what I read is needed to register the script and to tell wordpress to use Jquery yet I keep recieving errors.
我添加了我阅读的内容来注册脚本并告诉 wordpress 使用 Jquery,但我一直收到错误。 Before it wasnt registering that it needed jquery.
在它没有注册它需要 jquery 之前。 When i added the wptuts_script it won't recongnize that now.
当我添加 wptuts_script 时,它现在无法识别。 Did i put this code in the wrong place?
我是不是把这段代码放错了地方? here is the file path to the js file
html/wp-content/themes/metis/js
Basically I want to add this js (using jquery):这是js文件
html/wp-content/themes/metis/js
的文件路径基本上我想添加这个js(使用jquery):
document.ready(function() {
function wptuts_scripts_with_jquery()
{
// or
// Register the script like this for a theme:
wp_register_script( 'custom-script', get_template_directory_uri() . '/js/test.js', array( 'jquery' ));
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );
function updatebtm() {
var pos = $(window).scrollTop();
console.log(pos);
if (pos >= 800) {
$('.portfolio-top').css('display', 'none');
} else {
$('.portfolio-top').css('display', 'block');
}
} $(window).bind('scroll', updatebtm);
});
The problem is that you're completely mixing PHP and JavaScript.问题是你完全混合了 PHP 和 JavaScript。 You should be doing two things:
你应该做两件事:
Your JavaScript also has some syntax errors.你的 JavaScript 也有一些语法错误。
The following is an example of what your files could look like:以下是您的文件可能是什么样子的示例:
JavaScript: JavaScript:
(function($) {
$(document).ready(function() {
function updatebtm() {
var pos = $(window).scrollTop();
console.log(pos);
if (pos >= 800) {
$('.portfolio-top').css('display', 'none');
} else {
$('.portfolio-top').css('display', 'block');
}
}
$(window).bind('scroll', updatebtm);
});
})(jQuery);
PHP (in functions.php ) : PHP (在functions.php 中) :
function wptuts_scripts_with_jquery() {
wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/test.js', array( 'jquery' ));
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );
There is a Very easy way to add JS using a plugin called "Code Embed".有一种非常简单的方法可以使用名为“代码嵌入”的插件来添加 JS。
Use Following Link see step by step of using this plugin.使用以下链接查看使用此插件的步骤。 Guide
指导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.