【问题标题】:503 error trying to enqueue custom jQuery script in WordPress尝试在 WordPress 中将自定义 jQuery 脚本排入队列时出现 503 错误
【发布时间】:2019-10-18 02:36:19
【问题描述】:

我正在尝试设置我的第一个自定义 WordPress 主题。我正在使用“front-page.php”来设置标准登录页面,并尝试使用一些 jQuery 文件(所有这些文件都是以兼容模式编写的)。当 functions.php 文件被写入我期望它工作的地方时,它给了我一个 503 错误,并且该站点只适用于我知道不正确的代码。

我尝试过排队与注册、添加管理代码、停用和重新注册 jquery,并将所有“add_action”添加到一行中。我错过了什么?还没有任何类型的 JS 致力于这个主题。这是functions.php文件:

<?php

add_theme_support( 'custom-logo' );
add_theme_support( 'custom-background' );
add_theme_support( 'title-tag' );

function modify_jquery() {
    wp_dequeue_script('jquery');
    wp_deregister_script('jquery');

    wp_register_script('jquery-custom', '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', false, '1.11.3', 'true');
    wp_enqueue_script('jquery-custom');
}
add_action( 'wp_enqueue_scripts', 'modify_jquery' );

function theme_scripts() {
    wp_enqueue_script( 'navbar', get_template_directory_uri() . '/scripts/navbar.js' );
    wp_enqueue_script( 'fixedheader', get_template_directory_uri() . '/scripts/fixedheader.js', array( 'jquery' ), null, true );
    wp_enqueue_script( 'skills', get_template_directory_uri() . '/scripts/skills.js', array( 'jquery' ), null, true );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );

function theme_admin_scripts() {
    wp_enqueue_script( 'navbar', plugin_dir_url( __FILE__ ) . '/scripts/navbar.js' );
    wp_enqueue_script( 'fixedheader', plugin_dir_url( __FILE__ ) . '/scripts/fixedheader.js', array( 'jquery' ), null, true );
    wp_enqueue_script( 'skills', plugin_dir_url( __FILE__ ) . '/scripts/skills.js', array( 'jquery' ), null, true );
}
add_action( 'admin_enqueue_scripts', 'theme_admin_scripts' );

function register_navigation() {
    $args = array(
    'description'   => '',
    'class'         => '',
    'before_widget' => '',
    'after_widget'  => '',
    'before_title'  => '<h3 class="widget-title">',
    'after_title'   => '</h3>' );

  register_navigation($args);
}
add_action( 'widgets_init', 'register_navigation' );

?>

我希望我的网站有一个导航菜单,当阅读器向下滚动时会下降(这在我将其设为 wordpress 主题文件之前在静态页面中工作),以及其他一些 jQuery 功能。该网站的其余部分工作正常,图片加载等,但我无法成功添加任何脚本。非常感谢任何帮助!

【问题讨论】:

    标签: php jquery wordpress


    【解决方案1】:

    好的,你不需要重新注册 jQuery 脚本。默认情况下可用的那个应该可以工作。

    admin_enqueue_scripts 上的注册也让我有点困惑,因为这会在仪表板端注册脚本,如果您从插件内部注册脚本,plugin_dir_url( __FILE__ ) 很有用。

    考虑到你有默认的 jQuery 文件并且句柄没有被注销,下面的代码应该适合你。

    add_action('wp_enqueue_scripts', 'theme_scripts');
    function theme_scripts() {
        wp_enqueue_script('theme-navbar', get_stylesheet_directory_uri().'/scripts/navbar.js', array('jquery), $ver = false, $in_footer = true );
    }
    

    get_stylesheet_directory_uri() //points to the directory in your active theme where stylesheet is located so the path will be something like '/wp-content/themes/YOURTHEME' 只需确保您的脚本路径正确,这应该可以正常工作。

    【讨论】:

    • 非常感谢您的回复!不幸的是,当我插入该代码时,它仍然给我一个 503 错误......我怀疑我的 WP 实例中没有正确配置某些东西,或者我的主机由于某种原因无法处理它,这其中任何一个都是真的吗?跨度>
    • 您的服务器设置很可能搞砸了。尝试联系您的托管服务提供商支持系统。如果所有其他方法都失败了,您始终可以在页脚中使用脚本标记来将您的脚本排入队列。诚然,这不是理想的解决方案,但它确实有效。只需确保在 wp_footer 函数之后添加脚本标签
    猜你喜欢
    • 2018-07-17
    • 2021-05-11
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2017-03-17
    • 2017-06-20
    相关资源
    最近更新 更多