简体   繁体   English

将自定义 Js 添加到 Wordpress 主题

[英]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:你应该做两件事:

  1. Add a custom JavaScript file to your theme将自定义 JavaScript 文件添加到您的主题
  2. Register / enqueue that .js file in a plugin or functions.php在插件或functions.php中注册/排队该.js文件

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。

  1. Once you have the plugin installed start a new post or page.安装插件后,开始一个新的帖子或页面。
  2. In the Custom Fields meta box enter a name of CODE1 and your embed code as the value.在自定义字段元框中输入名称 CODE1 和您的嵌入代码作为值。 Save this.保存这个。
  3. In your post add {{CODE1}} where you wish the embed code to appear.在您的帖子中添加 {{CODE1}} 您希望嵌入代码出现的位置。

Use Following Link see step by step of using this plugin.使用以下链接查看使用此插件的步骤。 Guide 指导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM