admin管理员组文章数量:1336656
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' );
}
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' );
}
Share
Improve this question
asked Jun 18, 2020 at 13:14
ralphjsmitralphjsmit
4026 silver badges23 bronze badges
4
|
1 Answer
Reset to default 2You are already doing things properly (in enqueueing CSS and JS files only on specific template) and is_page_template()
is indeed a function you can use to check if the current or a specific Page or post is using a certain Page Template — i.e. a template having the PHP comment Template Name: <name here>
at the top of the file.
However, the function works only with static Pages or posts that have an entry in the WordPress' posts database table which then enables you to select a Page Template for that specific Page or post. Or which enables you to programmatically (e.g. using the update_post_meta()
function) set the page template for that Page or post:
update_post_meta( 123, '_wp_page_template', 'your-template.php' ); // 123 is the post ID
But for dynamic pages like the home page when it's set as the blog page which displays the latest posts, is_page_template()
will likely return false
, so for the blog page, I'd just use is_home()
which is one of the many conditional tags in WordPress:
function my_theme_styles() {
if ( is_home() ) {
// Enqueue styles for the home/blog page.
}
// Enqueue other styles.
}
add_action( 'wp_enqueue_scripts', 'my_theme_styles' );
function my_theme_scripts() {
if ( is_home() ) {
// Enqueue scripts for the home/blog page.
}
// Enqueue other scripts.
}
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
So for such dynamic pages, it's not easy to determine the exact template being used, but instead of checking against the template, you could just use the relevant conditional tag like is_home()
, is_front_page()
, is_category()
, etc. :)
本文标签: phpHow to enqueue CSS and JS only on specific template
版权声明:本文标题:php - How to enqueue CSS and JS only on specific template? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742334740a2455399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
add_action( 'wp_enqueue_scripts', 'amb_styles' );
hook. It is inside theamb_styles()
. – ralphjsmit Commented Jun 18, 2020 at 14:36home.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)? – Sally CJ Commented Jun 18, 2020 at 14:42is_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! – ralphjsmit Commented Jun 18, 2020 at 14:44