简体   繁体   中英

jquery not loading in WordPress

I have this code...

<?php
    function add_jquery() {
       wp_enqueue_script( 'jquery' );
    }    
    add_action('init', 'add_jquery');
?>

<script type="text/javascript">
    if (typeof jQuery == 'undefined') {  
        alert('jQuery IS NOT loaded')  
    } else {
        alert('jQuery IS loaded')
    }
</script>

<!-- Let's try loading jQuery the WRONG WAY when doing it in WordPress -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"></script>

<script type="text/javascript">
    if (typeof jQuery == 'undefined') {  
        alert('jQuery IS NOT loaded')  
    } else {
        alert('jQuery IS loaded')
    }
</script>

The first alert says "jQuery IS NOT loaded" and the second says "jQuery IS loaded". Why does jQuery not load when using the first method?

where is your add_jquery function? It won't work in a wordpress template. it needs to go in functions.php or someplace where it will run before the loop.

its because this line loads jQuery

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"></script>

After this line jQuery is available.

what the notes are saying this is the incorrect way to load jQuery into wordpress. The typical "right" way is to add a call into your header.

If you enqueue something in wordpress - particularly jQuery, you have to enqueue everything. Otherwise your libraries will not have access to jQuery.

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