I'm busy with my home.php template and I need a few CSS/JS files that are only loaded on that specific template. I tried doing it with
if ( is_page_template( 'home.php' ) ) { //ENQUEUE CODE }
,
but somehow that doesn't work. I don't have the slightest idea where the error is. When I remove the if-condition, all files are loaded correctly, but when I add the condition, none of them is loaded.
Help on how to fix this is much appreciated!
if ( is_page_template( 'home.php' ) ) {
wp_enqueue_style( 'home', get_template_directory_uri() . '/assets/css/home.css', array(), filemtime(get_template_directory() . '/assets/css/home.css'), 'all' );
if ( 'hide' !== get_theme_mod( 'sidebar_position', 'hide' ) ) {
wp_enqueue_style( 'sidebar', get_template_directory_uri() . '/assets/css/sidebar.css', array(), filemtime(get_template_directory() . '/assets/css/sidebar.css'), 'all' );
}
wp_register_script( 'blog-ajax-script', get_stylesheet_directory_uri(). '/assets/js/blog-ajax-script.js', array('jquery'), filemtime(get_template_directory() . '/assets/js/blog-ajax-script.js'), true );
$script_data_array = array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'security' => wp_create_nonce( 'load_more_posts' ), );
wp_localize_script( 'blog-ajax-script', 'blog', $script_data_array );
wp_enqueue_script( 'blog-ajax-script' );
}
add_action( 'wp_enqueue_scripts', 'amb_styles' );
hook. It is inside theamb_styles()
.home.php
actually a Page template, i.e. it has theTemplate Name:
header and that the page you're checking is indeed using that template? Perhaps you're just looking foris_home()
(for checking if the current page is the home page)?is_home()
did the trick. What do you mean with theTemplate Name:
header? And do probably you have a list with all similar conditions likeis_home()
? If you add it as an answer, I'll mark it as correct @SallyCJ. Thanks a lot!