How to load javascript on custom page template?

add_action('wp_enqueue_scripts','Load_Template_Scripts_wpa83855');
function Load_Template_Scripts_wpa83855(){
    if ( is_page_template('template-scrollcontent.php') ) {
        wp_enqueue_script('my-script', '/wp-content/themes/theme_name/js/shadowbox/shadowbox.js');
       wp_enqueue_script('my-scripts', '/wp-content/themes/theme_name/js/jquery.tools.minc619.js');
    } 
}

Or Load JavaScript only on specific WordPress pages

//Register hook to load scripts
add_action('wp_enqueue_scripts', 'my_theme_load_scripts');

//Load scripts (and/or styles)
function my_theme_load_scripts(){
   wp_enqueue_script('jquery');
   wp_enqueue_script('my_first_script', get_template_directory_uri() . '/includes/js/my_first_script.js');
   wp_enqueue_script('my_second_script', get_template_directory_uri() . '/includes/js/my_second_script.js');

   if(is_page()){ //Check if we are viewing a page
	global $wp_query;

        //Check which template is assigned to current page we are looking at
        $template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
	if($template_name == 'slider-portfolio.php'){
           //If page is using slider portfolio template then load our slider script
	   wp_enqueue_script('my_third_script', get_template_directory_uri() .'/includes/js/my_third_script.js');		
	}
   }
}
The following two tabs change content below.

Md Jahed

Latest posts by Md Jahed (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *