简体   繁体   中英

Wordpress linking css and js to my theme doesn't work using wp_enqueue_scripts

This has been giving me fits. I've been trying to build my first custom theme and I'm stuck pretty early on... I've been following tutorials and checking the Wordpress Codex, but I'm pretty new to this. It's not including a stylesheet at all, and when I try to include the js, I get an error "This Page Isn't Working".

<?php

function jw_script_enqueue() {

wp_enqueue_style('custom-style', get_template_directory_uri() . '/css/jw-custom.css', array(), false, 'all' );
we_enqueue_script('custom-js', get_template_directory_uri() . '/js/jw-custom.js', false, true)

}

add_action( 'wp_enqueue_scripts', 'jw_script_enqueue');

Here is my header:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JW Custom</title>
        <?php wp_head(); ?>
    </head>

    <body>

and here is my footer:

        <footer>
            © 2018 JW Custom
        </footer>

        <?php wp_footer(); ?>
    </body>
</html>

My filepaths are correct. Any help would be greatly appreciated. What should I change?

First thing's first, make sure your file locations are accurate, and you're not getting 404 errors in your console.

That said, you've got a typo, we_enqueue_script should be wp_enqueue_script - and you may actually be getting an undefined function error.

You also don't need to include the additional arguments in the wp_enqueue_ functions, and you're missing the $deps argument in your scripts call.

Also, unless you're building a parent theme, you may want to stick with get_stylesheet_directory_uri() instead of get_template_directory_uri() .

Lastly, custom-script isn't a unique enough handle and you may actually be getting conflicts from a plugin or parent theme if it's active.

Try the following:

add_action( 'wp_enqueue_scripts', 'jw_script_enqueue');
function jw_script_enqueue() {
    wp_enqueue_style( 'jw-custom-style', get_stylesheet_directory_uri() .'/css/jw-custom.css' );
    wp_enqueue_script( 'jw-custom-js', get_stylesheet_directory_uri() .'/js/jw-custom.js );
}

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