loading a different stylesheet for a specific page template

Hi,

I am trying to load a different stylesheet for my page template "page_prevdefault.php" called prevdefault.css. The top of the functions file in my child theme looks like this

<?
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
    function chld_thm_cfg_parent_css() {
        wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css' );
    }
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css' );

// END ENQUEUE PARENT ACTION

function lander_work_script() {
if ( is_page_template('page-templates/page_prevdefault.php') ) {
wp_enqueue_style( 'lander-work-style', get_stylesheet_directory_uri() . '/prevdefault.css');
}
}

add_action('wp_enqueue_scripts', 'lander_work_script',15,0);

When I use the template prevdefault, style.css is still being used. Any ideas on what I should be doing?

Thanks

  • Sajid

    Hi @Lukas Feddern

    Hope you are doing good today :slight_smile:

    You should not need to use two functions to enqueue, instead add your template CSS in same function and wrap with if condition.

    I updated the code for you, try below code instead and let me know the results.

    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
    if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
        function chld_thm_cfg_parent_css() {
    
    		if ( is_page_template('page-templates/page_prevdefault.php') ) {
    			wp_enqueue_style( 'lander-work-style', get_stylesheet_directory_uri() . '/prevdefault.css');
    		}else{
    		    wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css' );
    		}
        }
    endif;
    add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css' );

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.