How can I enqueue script based on template part?

I want to be able to load scripts and stylesheets in the head and foot based on wordpress get template part

I have on in my front-page.php the following code:

<?php 
/*
Template Name: Home Page
*/

get_header();

$loop = new WP_Query( array( 'post_type' => 'home_page','posts_per_page'=>1, 'orderby' => 'menu_order', 'order' => 'DESC') ); 
while ( $loop->have_posts() ) : $loop->the_post(); 

if( get_field('home_page_template_type') == 'Bookcase') {

    get_template_part( 'bookcase' ); 

}elseif (get_field('home_page_template_type') == 'Bookers') {

    get_template_part('bookers' ); 

}

endwhile;

get_footer();

Now in my functions php I want to do this

// Register Script
if (!function_exists('theme_scripts')) {
    function villarosa_theme_scripts() {
        global $wp_scripts;

        if ('bookcase.php') {
            // Load our main stylesheet.
            wp_deregister_style( 'bookcase-styles' );
            wp_register_style( 'bookcase-styles',trailingslashit( THEME_URI ) .'bookcase/css/style.css', false, '1.0' );
            wp_enqueue_style( 'bookcase-styles' );

            // jQuery (necessary for Bootstrap's JavaScript plugins) 
            wp_deregister_script( 'jquery' );
            wp_register_script( 'jquery', trailingslashit( THEME_URI ) .'bookcase/js/jquery.js', false, '1.0', true );
            wp_enqueue_script( 'jquery' );

            wp_deregister_script( 'jquery-migrate' );
            wp_register_script( 'jquery-migrate', trailingslashit( THEME_URI ) .'bookcase/js/jquery-migrate-1.2.1.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-migrate' );

            wp_deregister_script( 'jquery-easing' );
            wp_register_script( 'jquery-easing', trailingslashit( THEME_URI ) .'bookcase/js/jquery.easing.1.3.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-easing' );

            wp_deregister_script( 'script' );
            wp_register_script( 'script', trailingslashit( THEME_URI ) .'bookcase/js/script.js', false, '1.0', true );
            wp_enqueue_script( 'script' );

            wp_deregister_script( 'jquery-equalheights' );
            wp_register_script( 'jquery-equalheights', trailingslashit( THEME_URI ) .'bookcase/js/jquery.equalheights.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-equalheights' );

            wp_deregister_script( 'jquery-ui-totop' );
            wp_register_script( 'jquery-ui-totop', trailingslashit( THEME_URI ) .'bookcase/js/jquery.ui.totop.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-ui-totop' );

            wp_deregister_script( 'superfish' );
            wp_register_script( 'superfish', trailingslashit( THEME_URI ) .'bookcase/js/superfish.js', false, '1.0', true );
            wp_enqueue_script( 'superfish' );

            wp_deregister_script( 'jquery-mobilemenu' );
            wp_register_script( 'jquery-mobilemenu', trailingslashit( THEME_URI ) .'bookcase/js/jquery.mobilemenu.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-mobilemenu' );

            wp_deregister_script( 'sForm' );
            wp_register_script( 'sForm', trailingslashit( THEME_URI ) .'bookcase/js/sForm.js', false, '1.0', true );
            wp_enqueue_script( 'sForm' );

        } else if ('bookers.php') {
            // Load our main stylesheet.
            wp_deregister_style( 'bookers-styles' );
            wp_register_style( 'bookers-styles',trailingslashit( THEME_URI ) .'bookers/css/style.css', false, '1.0' );
            wp_enqueue_style( 'bookers-styles' );

            wp_deregister_style( 'stuck' );
            wp_register_style( 'stuck',trailingslashit( THEME_URI ) .'bookers/css/stuck.css', false, '1.0' );
            wp_enqueue_style( 'stuck' );

            // jQuery (necessary for Bootstrap's JavaScript plugins) 
            wp_deregister_script( 'jquery' );
            wp_register_script( 'jquery', trailingslashit( THEME_URI ) .'bookers/js/jquery.js', false, '1.0', true );
            wp_enqueue_script( 'jquery' );

            wp_deregister_script( 'jquery-migrate' );
            wp_register_script( 'jquery-migrate', trailingslashit( THEME_URI ) .'bookers/js/jquery-migrate-1.2.1.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-migrate' );

            wp_deregister_script( 'jquery-easing' );
            wp_register_script( 'jquery-easing', trailingslashit( THEME_URI ) .'bookers/js/jquery.easing.1.3.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-easing' );

            wp_deregister_script( 'script' );
            wp_register_script( 'script', trailingslashit( THEME_URI ) .'bookers/js/script.js', false, '1.0', true );
            wp_enqueue_script( 'script' );

            wp_deregister_script( 'jquery-equalheights' );
            wp_register_script( 'jquery-equalheights', trailingslashit( THEME_URI ) .'bookers/js/jquery.equalheights.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-equalheights' );

            wp_deregister_script( 'jquery-ui-totop' );
            wp_register_script( 'jquery-ui-totop', trailingslashit( THEME_URI ) .'bookers/js/jquery.ui.totop.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-ui-totop' );

            wp_deregister_script( 'superfish' );
            wp_register_script( 'superfish', trailingslashit( THEME_URI ) .'bookers/js/superfish.js', false, '1.0', true );
            wp_enqueue_script( 'superfish' );

            wp_deregister_script( 'tmStickUp' );
            wp_register_script( 'tmStickUp', trailingslashit( THEME_URI ) .'bookers/js/tmStickUp.js', false, '1.0', true );
            wp_enqueue_script( 'tmStickUp' );

            wp_deregister_script( 'jquery-mobilemenu' );
            wp_register_script( 'jquery-mobilemenu', trailingslashit( THEME_URI ) .'bookers/js/jquery.mobilemenu.js', false, '1.0', true );
            wp_enqueue_script( 'jquery-mobilemenu' );
        }
    }
}

However the scripts are loading for my first condition but for else if they are not? Why is this the case?

Related posts

Leave a Reply

2 comments

  1. if tests for something tangible if you pass it a string or variable. e.g. if($var) will equate to if($var != null) so your function is not progressing beyond if ('bookcase.php') as it is always true (you are not testing if this file exists, you are testing whether its not null)

    Probably what you want is:

     function villarosa_theme_scripts($type) {
        if($type==='bookcase'):
           // enqueue scripts
        elseif($type==='bookers':
           // enqueue scripts
        endif;
     }
    

    and you can call it on your template directly *place this code before your header……

    if( get_field('home_page_template_type') == 'Bookcase') {
    
            get_template_part( 'bookcase' ); 
            villarosa_theme_scripts('bookcase');
    
    }elseif (get_field('home_page_template_type') == 'Bookers') {
    
            get_template_part('bookers' ); 
            villarosa_theme_scripts('bookers');
    
    }
    get_header();
    
  2. I don’t see where you are calling villarosa_theme_scripts, but in any case:

    if ('bookcase.php')
    

    will always evaluate to true. You need to be testing some variable (I assume it’s a WordPress template name), like:

    if ($template == 'bookcase.php')