简体   繁体   中英

WordPress - Trouble with enqueue-ing scripts/CSS

So, I've not used Wordpress in a long time, and I'm trying to get back into the swing of things. Apparently the correct way to deal with external CSS/JS is to enqueue them using wp_register_style , wp_enqueue_style , wp_register_script , and wp_enqueue_script ... but they're not working for me at all.

The following is my current functions.php :

<?php

    // Enqueue necessary scripts/etc.
    function wasd_styles() {
        wp_register_style('semantic-css', get_stylesheet_directory_uri() . '/lib/semantic.min.css');
        wp_enqueue_style('semantic-css');
    }

    function wasd_scripts() {
        wp_register_script('semantic-js', get_template_directory_uri() . '/lib/semantic.min.js');
        wp_enqueue_script('semantic-js');
    }

    add_action('wp_enqueue_scripts', 'wasd_styles');
    add_action('wp_enqueue_scripts', 'wasd_scripts');

?>

Is there something I'm doing wrong? As far as I can tell, it should be injecting the proper calls to grab CSS/JS into the tag of my page, but..it's not.

The following are the two files I'm attempting to render, I threw this together in like 5 minutes to attempt to test Semantic UI, but it's just not working at all, no style is being rendered, and is empty upon source inspection.

index.php

<?php get_header(); ?>

<div class="ui main container">
    <h2 class="ui dividing header">This is a test</h2>
</div>

header.php

<div class="ui top fixed inverted menu">
    <div class="item">Test</div>
</div>

From the code you have shared, your theme does not support the use of wp_enqueue_script() . This function simply registers the scripts and gets it ready for insertion into the DOM. This is inserted via a hook, which doesn't appear to be present in your theme. You would need to add the following into your header.php file. This would typically be added between the <head></head> tags.

<?php wp_head(); ?>

Also, you should have a similar hook in your footer.php file, typically just before the closing </body> tag. This hook would be:

<?php wp_footer(); ?>

Without these, wp_enqueue_script() and wp_enqueue_style() will not work.

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