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.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统