admin管理员组

文章数量:1122832

So I've got a bit of an issue, and not sure what the problem is. I'm trying to enqueue javascript in the footer of my Wordpress site. The footer looks like this:

    </footer>
</div>

<?php wp_footer(); ?>

<script>
    jQuery(document).foundation();
</script>
</body>
</html>

So the enqueued script SHOULD be put in the wp_footer call, putting the script just above where I actually call it. But looking at the source code of the file, I get this:

</footer>
</div>







<script>
jQuery(document).foundation();
</script>
<script type='text/javascript' src='.3.1/js/foundation.min.js?ver=4.3.3'></script>
</body>
</html>

For some reason, the script in question is being placed RIGHT BEFORE the closing body tag.

I verified the wp_footer is in the correct place, so I'm not sure what would cause this to happen. If I enqueue the script to the head, it works just fine, but I'm just curious as to why this behavior is happening.

UPDATE (NEW CODE): Sorry for the confusion. Here's the functions.php portion of the script enqueue method:

function load_cornerstone_scripts() {
    wp_enqueue_script(
        'foundation_js',
        get_template_directory_uri() . '/js/foundation.min.js',
        array('jquery'),
        '4.3.1',
        true
    );

}

add_action('wp_enqueue_scripts', 'load_cornerstone_scripts',0);

So to be clear..the original two blocks of code are showing 1) What the php file footer.php looks like, and 2) what the source code is rendered as.

So I've got a bit of an issue, and not sure what the problem is. I'm trying to enqueue javascript in the footer of my Wordpress site. The footer looks like this:

    </footer>
</div>

<?php wp_footer(); ?>

<script>
    jQuery(document).foundation();
</script>
</body>
</html>

So the enqueued script SHOULD be put in the wp_footer call, putting the script just above where I actually call it. But looking at the source code of the file, I get this:

</footer>
</div>







<script>
jQuery(document).foundation();
</script>
<script type='text/javascript' src='http://www.thetestsite.com/wp-content/themes/cornerstone-4.3.1/js/foundation.min.js?ver=4.3.3'></script>
</body>
</html>

For some reason, the script in question is being placed RIGHT BEFORE the closing body tag.

I verified the wp_footer is in the correct place, so I'm not sure what would cause this to happen. If I enqueue the script to the head, it works just fine, but I'm just curious as to why this behavior is happening.

UPDATE (NEW CODE): Sorry for the confusion. Here's the functions.php portion of the script enqueue method:

function load_cornerstone_scripts() {
    wp_enqueue_script(
        'foundation_js',
        get_template_directory_uri() . '/js/foundation.min.js',
        array('jquery'),
        '4.3.1',
        true
    );

}

add_action('wp_enqueue_scripts', 'load_cornerstone_scripts',0);

So to be clear..the original two blocks of code are showing 1) What the php file footer.php looks like, and 2) what the source code is rendered as.

Share Improve this question edited Sep 4, 2013 at 18:48 Chris Klongpayabal asked Sep 4, 2013 at 15:48 Chris KlongpayabalChris Klongpayabal 591 silver badge6 bronze badges 5
  • 1 I am not seeing any issue. In your code your wp_footer() function IS placed before your closing body tag so it is correct that your enqueue will also show before the closing body tag...What is the issue there? – user23654 Commented Sep 4, 2013 at 16:00
  • If you look at the code, the wp_footer code should execute, and THEN my jQuery(document).foundation(); script should fire. As it is, the enqueued script is being put AFTER my jQuery(document).foundation(); script call, which is causing a javascript error because the foundation function hasn't been declared yet. – Chris Klongpayabal Commented Sep 4, 2013 at 16:16
  • But it is contradicting. In one instance you're enqueuing the script and the other you're hard coding it in your footer...Why not enqueue both script one after the other or even with dependencies? – user23654 Commented Sep 4, 2013 at 16:30
  • It's the same thing. The top one is how my php code looks, and the bottom is how it's rendered. According to my php code, the cornerstone script SHOULD be loaded before my jquery function call. but in the second code block, the cornerstone is being put AFTER my jquery function call. The second code block is the resulting code when php is parsed and html is generated. – Chris Klongpayabal Commented Sep 4, 2013 at 18:43
  • Are you sure you're working on the correct theme files? Are you sure it is not cached or something like that? – hamdirizal Commented Apr 28, 2019 at 6:53
Add a comment  | 

1 Answer 1

Reset to default 0

You want something like this.

// assuming you want to load this only on frontend
if ( ! is_admin() )
     add_action( 'wp_enqueue_scripts', 'wpse_112876_load_scripts' );

function wpse_112876_load_scripts() {
     wp_enqueue_script( 'my_foundation', get_template_directory_uri() . '/js/foundation.min.js', null, '4.3.3', true );

     wp_enqueue_script( 'my_foundation_init', get_template_directory_uri() . '/js/foundation_init.js', array( 'my_foundation' ), null, true );
}

本文标签: Enqueueing Script to footer puts it at the very bottom