简体   繁体   中英

load jquery inline script without dependency in wordpress

I'm rather new to the whole wordpress scene so I dont"t really know the do's and dont's of wordpress of whats possible and what not.

I am trying to build a custom theme from a pre-build one called illdy. And i want to load a jquery script in the footer for changing the menu styles on scroll by toggling a class. but the script won't load. i've used wp_enqueu_script and a link to the google ajax library, and put my script in a function in my functions.php to include it in the footer.

i don't know if this is even possible, any help will be appreciated.

Did you follow this?

Usage

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );

Links a script file to the generated page at the right time according to the script dependencies, if the script has not been already included and if all the dependencies have been registered. You could either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script.

This is the recommended method of linking JavaScript to a WordPress generated page.

Please note:

The function should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end of the site, like in the examples above. To call it on the administration screens, use the admin_enqueue_scripts action hook. For the login screen, use the login_enqueue_scripts action hook. Calling it outside of an action hook can lead to problems, see the ticket #11526 for details.

function themeslug_enqueue_style() {
    wp_enqueue_style( 'core', 'style.css', false ); 
}

function themeslug_enqueue_script() {
    wp_enqueue_script( 'my-js', 'filename.js', false );
}

add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );

To make a theme compatible with child themes, you can use functions to determine the correct path to the core stylesheets ( without using @import in the CSS ):

function themeslug_enqueue_style() {
    if ( is_child_theme() ) {
        // load parent stylesheet first if this is a child theme
    wp_enqueue_style( 'parent-stylesheet', trailingslashit( get_template_directory_uri() ) . 'style.css', false );
    }
    // load active theme stylesheet in both cases
    wp_enqueue_style( 'theme-stylesheet', get_stylesheet_uri(), false );
}

add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );

From:

https://developer.wordpress.org/reference/functions/wp_enqueue_script/

and

https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM