How do i load javascript on a specific custom post template via functions.php?

admin2025-06-05  2

I want to load some javascript on a custom post template called progress-posts.php. Using:

if (is_page_template( 'progress-posts.php' )) { 
        wp_enqueue_script( 'progress-box', get_stylesheet_directory_uri() . '/js/progress-boxes.min.js', array(), null, true );
    }

...didn't seem to do anything. I thought maybe because this is a template for posts and not pages. The method I've found that works is:

if (is_single()) { 
        wp_enqueue_script( 'progress-box', get_stylesheet_directory_uri() . '/js/progress-boxes.min.js', array(), null, true );
    }

However I'd like the option of adding single posts to my site in the future which don't require this javascript to not load it.

Here's the full section in my functions file as it is now for clarity:

// Add Javascript files here
function enqueue_my_scripts() {
    if (is_page_template( 'page-templates/home.php' )) { 
        wp_enqueue_script( 'aosjs', get_stylesheet_directory_uri() . '/js/aos.js', array(), null, true );
        wp_enqueue_script( 'aosjsinit', get_stylesheet_directory_uri() . '/js/aos-init.min.js', array(), null, true );
    }
    if (is_page_template( 'page-templates/progress.php' )) { 
        wp_enqueue_script( 'progress-box', get_stylesheet_directory_uri() . '/js/progress-boxes.min.js', array(), null, true );
    }
    if (is_single()) { 
        wp_enqueue_script( 'progress-box', get_stylesheet_directory_uri() . '/js/progress-boxes.min.js', array(), null, true );
    }
    wp_enqueue_script( 'humburgersjs', get_stylesheet_directory_uri() . '/js/hamburgers.min.js', array(), null, true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_my_scripts' );

How would I load it specifically for the custom post template I made and nothing else?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749105733a316409.html

最新回复(0)