douzen1896 2019-06-02 04:03
浏览 66

503尝试在WordPress中排队自定义jQuery脚本时出错

I am trying to set up my very first custom WordPress theme. 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). 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.

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. What am I missing? No JS of any sort has yet worked on this theme. Here is the functions.php file:

<?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. 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!

  • 写回答

1条回答 默认 最新

  • doulao5916 2019-06-02 14:01
    关注

    Okay for start you don't need to do re-register jQuery script. 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.

    Considering that you have the default jQuery file and the handle isn't de registered, the following code should work for you.

    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.

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码