$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How do i load javascript on a specific custom post template via functions.php?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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?

Post a comment

comment list (0)

  1. No comments so far