0

I have a page template which is applied to a specific page on my wordpress page. Usually for all of my other scripts i put them in my footer.php and they load from there beautifully. On this specific page though I want to load a script that only should be running on this page. So in my template i include it like this:

<?php
/**
* Template Name: Template
*/
?>
<?php get_header(); ?>

<script src="<?php echo get_template_directory_uri(); ?>/js/teacher-signup.js"></script>

<?php get_footer( ); ?>

Unfortunately when doing that I only get errors like:

Uncaught TypeError: $(...).whatever is not a function

Is there some way in Wordpress to include a couple of scripts on a specific template only? Or am I missing something?

Thanks in advance

Triphys
  • 53
  • 9
  • 1
    Are you loading jQuery in the footer as well? If so, you might want to move it to the header. It seems that you're trying to use the jQuery wrapper before jQuery is being loaded. – Baruch May 17 '17 at 15:48
  • Yes, I was loading it in the footer for this example but tried in the header as well, but it generated a similar error. Although the replies below solved my issues. Thanks! – Triphys May 18 '17 at 09:26

2 Answers2

2

You can make if conditional is_page() in certain page id / page slug for example

<?php if (is_page( 1 )) : ?>
<script src="<?php echo get_template_directory_uri(); ?>/js/teacher-signup.js"></script>
<?php endif; ?>

You can put it on your footer.php theme

rheeantz
  • 960
  • 8
  • 12
2

register the script like wp recommends:

wp_register_script('your_file_name', get_template_directory_uri() . '/assets/js/some_file.js', false, null, true);

than enqueue the registered script for that specific page/template

if (is_page('page_name') ) {
        wp_enqueue_script('your_file_name');

        }

you can also add a conditional for the page template name if you want:

if (is_page_template("template_name.php')
Society43
  • 737
  • 9
  • 12