【问题标题】:How would I include this jquery code to wordpress?我如何将此 jquery 代码包含到 wordpress 中?
【发布时间】:2012-02-20 22:04:15
【问题描述】:

几周以来,我一直在想办法解决这个问题。如何在 wordpress 中包含 jquery ......所以我使用了 wp_enqeue_scripts(); 但后来我还是不明白。 所以例如我有这个jquery向下滑动代码..

$jQuery(document).ready(function() {

    $jQuery('#slide').slideDown();  

});

现在这段代码在我的静态 html 代码上完美运行,顺便说一下,其中包含幻灯片 ID..

那么我如何将该幻灯片包含到我的 wordpress 插件中? 这是我的插件代码

<?php
/*
Plugin Name: Jquery Test.
Plugin URI: http://wsplugins.com
Description: A plugin that increases your website traffic by floating facebook, twitter and google + share button. The buttons bounce and move over your page ensuring maximum attention by your visitors.
Author: Ronny Kibet.
Author URI: http://@.com
Version: 1.0
 */

谢谢。

【问题讨论】:

    标签: php javascript jquery wordpress jquery-ui


    【解决方案1】:

    你有几个选择。

    选项#1

    将代码粘贴到footer.php(或header.php)中。

    <script>
    $(function() {
        $jQuery('#slide').slideDown();  
    });
    </script>
    

    这将导致每个 ID 为“slide”的 DOM 元素在页面加载时滑动。

    选项#2

    使用WordPress' API,您可以在您的主题中创建一个方法来包含一个带有代码的JavaScript 文件。这比较复杂,需要了解 WordPress 钩子。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,但我得到了“有点”的工作。首先,您需要使用 wp_register_scripts 和脚本名称,然后执行 wp_enque_script("scriptname");... 我不明白为什么会这样,但我们只需要处理:-/

      【讨论】:

        【解决方案3】:

        您可能需要使用 noConflict,例如:

        <script type="text/javascript">
        
        var slide=jQuery.noConflict();
        
        slide(function() {
            slide('#slide').slideDown();  
        });
        </script>
        

        【讨论】:

          【解决方案4】:

          要在插件中将 wordpress 中的脚本排入队列,请使用:

          /**
          * Proper way to enqueue scripts
          */
          function theme_name_scripts() {
              wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js',    array('dependencyScriptThatNeedsToExecuteBeforeMyScriptName','forExample','jquery), '1.0.0', true );
          }
          
          add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
          

          【讨论】:

            【解决方案5】:

            我也在寻找相同的解决方案,但我自己找到了。

            在 js 目录中创建一个新文件 custom-functions.js,然后将要在该文件中加入队列的代码添加进去。现在将它排入functions.php

                wp_enqueue_script( 'custom-functions', get_template_directory_uri() . '/js/custom-functions.js', array(), '1.0', true );
            

            Bootstrap WordPress Snippets - wp_enqueue_script阅读完整教程

            【讨论】:

              【解决方案6】:
              函数主题名称脚本(){
                      wp_enqueue_script('script-name', plugins_url('/js/responsiveslides.min.js', __FILE__), array(),false, true);
              }
              add_action('wp_enqueue_scripts', 'theme_name_scripts');

              【讨论】:

              • 添加此代码的解释对您很有用
              猜你喜欢
              • 2018-06-07
              • 2013-06-03
              • 1970-01-01
              • 1970-01-01
              • 2017-11-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多