【问题标题】:How to load jQuery into WordPress properly如何将 jQuery 正确加载到 WordPress 中
【发布时间】:2014-04-09 16:50:04
【问题描述】:

我有一个 WordPress 网站,我想使用一个名为 DataTables 的 jQuery 插件。我一直在寻找将 jQuery 脚本和 DataTables 脚本加载到 WordPress 中的正确方法。

我知道我应该使用一些东西:wp_enqueue_script("jquery") - 但我不知道把它放在哪里或如何加载我需要的其他 jQuery 插件。

我尝试的最后一点是将其放入我的 WordPress 网站的 header.php 文件中:

<?php wp_enqueue_script("jquery"); ?>

<?php wp_head(); ?>

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript php jquery wordpress


    【解决方案1】:

    jQuery 包含在 WordPress 核心中,当您包含 javascript 时,您可以声明您的脚本依赖于作为 wp_enqueue_script 的第三个参数加载的 jquery:http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    使用 wp_enqueue_script 的正确方法是将其包含在附加到特定操作挂钩的回调中。当我开始使用 WordPress 时,这对我来说听起来非常复杂,但它实际上是一个非常好的系统,因为它允许您指定在 WordPress 初始化期间何时您的代码应该运行。

    所以,在你的 functions.php 或插件中,你添加你的钩子。

    add_action( 'wp_enqueue_scripts', 'my_js_include_function' );
    

    然后,在该函数中,您运行实际包含,并声明您的脚本依赖于 jquery。

    function my_js_include_function() {
        wp_enqueue_script( 'my_script.js', '/path/to/myscript.js', array('jquery') );
    }
    

    wp_ennqueue_script() 的第一个参数是您分配给脚本的任意名称。如果您随后声明另一个脚本依赖于原始脚本,那么您将在第三个参数中引用它。

    【讨论】:

    • 直接来自文档:Required to use core-bundled scripts rather than including their own version of that script. For example jQuery.。不要托管自己的 jQuery。
    【解决方案2】:

    如果您不能使用任何 WordPress 方法,您仍然可以在您的 extension_name.php 中手动包含您的 Jquery 插件,紧跟在您的 wp_enqueue_script("jquery"); 之后

    echo "<script src='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js'></script>";
    

    【讨论】:

      【解决方案3】:

      最好的方法是通过functions.php文件,

      网址:http://codex.wordpress.org/Function_Reference/wp_enqueue_script

      示例:

      /**
       * Proper way to enqueue scripts and styles
       */
      function theme_name_scripts() {
          wp_enqueue_style( 'style-name', get_stylesheet_uri() );
          wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
      }
      
      add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
      

      这段代码读到的是你正在创建一个名为“theme_name_scripts”的函数,现在你正在调用 wp_enqueue_script(),这需要 5 个参数“Script Name”、“URL”、“ARRAY”、“Version”、“True /False"(True 加载在 Header 中,False 加载在 Footer)...

      Add_action 部分是将其附加到 WordPress 的钩子。它是不言自明的,您需要它才能使功能正常工作。它说来自函数'theme_name_scripts'或函数名称的wp_enqueue_scripts ..

      祝你好运,如果您需要更多帮助,请尝试 Google 搜索“WordPress,入队脚本教程”

      【讨论】:

        【解决方案4】:

        在我的functions.php中:

        function mytheme_scripts() {
            wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/myscript.js', array('jquery'), false, true );
        }
        add_action( 'wp_enqueue_scripts', 'mytheme_scripts' );
        
        function custom_shortcode3() {
        
        echo "wp_enqueue_scripts();";
        }
        add_shortcode( 'totaldeprodutos', 'custom_shortcode3' );
        

        在我的 /js/myscript.js 中:

        <div id="show"></div>
        
        <script type="text/javascript" src="jquery.js"></script>
        
        <script type="text/javascript">
        
                            function total() {
                setInterval(function () {
                    $('#show').load('datatotaldeprodutos.php')
                }, 1000);
            };
        

        我需要显示短代码来运行 total()

        【讨论】:

          猜你喜欢
          • 2016-10-21
          • 1970-01-01
          • 1970-01-01
          • 2017-06-24
          • 2022-01-26
          • 1970-01-01
          • 2013-06-22
          • 2021-08-03
          • 1970-01-01
          相关资源
          最近更新 更多