[英]503 error trying to enqueue custom jQuery script in WordPress
I am trying to set up my very first custom WordPress theme. 我正在尝试设置我的第一个自定义WordPress主题。 I am using "front-page.php" to set up a standard landing page, and am trying to use a few jQuery files (all of which are written in compatibility mode).
我正在使用“ front-page.php”设置标准的登录页面,并尝试使用一些jQuery文件(所有这些文件均以兼容模式编写)。 When the functions.php file is written to where I expect it to work, it gives me a 503 error, and the site only works with code I know isn't correct.
当将functions.php文件写入我期望的工作位置时,它会给我503错误,并且该站点只能使用我知道不正确的代码工作。
I have tried enqueuing vs registering, adding admin code, de-activating and re-registering jquery, and adding all the "add_action"s into one line. 我尝试过排队vs注册,添加管理代码,停用和重新注册jquery,以及将所有“ add_action”添加到一行中。 What am I missing?
我想念什么? No JS of any sort has yet worked on this theme.
尚无任何JS处理此主题。 Here is the functions.php file:
这是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' );
?>
I would like for my site to have a navigation menu that comes down as the reader scrolls down (this worked in a static page before I made it a wordpress theme file), among some other jQuery functionality. 我希望我的网站具有一个导航菜单,该菜单会随着读者向下滚动而下降(在我将其制成wordpress主题文件之前,该菜单在静态页面中有效),以及其他一些jQuery功能。 The rest of the site works fine, pictures load, etc. but I have not been able to add any script successfully.
该网站的其余部分工作正常,图片加载等,但是我无法成功添加任何脚本。 Any help is greatly appreciated!
任何帮助是极大的赞赏!
Okay for start you don't need to do re-register jQuery script. 好的,您不需要重新注册jQuery脚本。 The one available by default should work.
默认情况下可用的一种应该起作用。
Also the registration on admin_enqueue_scripts
is a bit confusing form me as this would register script on dashboard side and the plugin_dir_url( __FILE__ )
is useful if you are registering scripts from inside a plugin. 另外,在
admin_enqueue_scripts
上的注册令我有些困惑,因为这将在仪表板侧注册脚本,如果您从插件内部注册脚本,则plugin_dir_url( __FILE__ )
非常有用。
Considering that you have the default jQuery file and the handle isn't de registered, the following code should work for you. 考虑到您具有默认的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'
Just make sure that your scripts path are correct and this should work just fine. 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'
只需确保您的脚本路径正确并且应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.