【问题标题】:How to include more then one JS in wordpress theme? [closed]如何在 wordpress 主题中包含多个 JS? [关闭]
【发布时间】:2014-08-10 20:14:57
【问题描述】:

我正在尝试在我的网站主题中添加两个 JS 文件:

  1. js/jscripts.js
  2. js/bootstrap-hover-dropdown.js

我正在使用的代码:

function wptuts_scripts_with_jquery()
{
    // Register the script like this for a plugin:
    wp_register_script( 'custom-script', plugins_url( '/js/custom-script.js', __FILE__ ), array( 'jquery' ) );
    // or
    // Register the script like this for a theme:
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/custom-script.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' );

通过使用这个功能,我只能添加一个 JS 文件,我如何添加第二个 JS 文件。我是 wordpress 和 PHP 的新手。需要帮忙!! :)

【问题讨论】:

  • 这些js文件在哪里?在主题 js 文件夹中?
  • 真的吗?您刚刚直接从 a tutorial 复制并粘贴了它,它准确地解释了如何做您所要求的!

标签: javascript php jquery wordpress


【解决方案1】:
function wptuts_scripts_with_jquery()
{

    // do this for plugin js
    wp_register_script( 'custom-script', plugins_url( '/js/jscripts.js', __FILE__ ), array( 'jquery' ) );
    wp_register_script( 'drop-down', plugins_url( '/js/bootstrap-hover-dropdown.js', __FILE__ ), array( 'jquery' ) );


    // or this for theme js
    wp_register_script( 'custom-script', get_template_directory_uri() . '/js/jscripts.js', array( 'jquery' ) );
    wp_register_script( 'drop-down', get_template_directory_uri() . '/js/bootstrap-hover-dropdown.js', array( 'jquery' ) );


    wp_enqueue_script( 'custom-script' );
    wp_enqueue_script( 'drop-down' );
}
add_action( 'wp_enqueue_scripts', 'wptuts_scripts_with_jquery' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-10
    • 2016-09-20
    • 2017-06-23
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多