如何为一个特定页面模板排队样式

I've got a page template which is acting as a 'Landing Page' and doesn't need specific styles from other areas of the website.

I've managed to remove the unwanted styles and add the new styles by targeting the page ID but I need it to only happen when it's a particular page template. I can't seem to get it to work when doing a check against the page template via the is_page_template() function.

In functions.php:

if ( !function_exists('scripts_and_css') ) {
    function scripts_and_css() {
        if(is_page(79806))
        {
            wp_enqueue_style('landingpage', get_stylesheet_directory_uri() . '/css/landing__page.css', '', null);
            wp_enqueue_script('landingpage', get_stylesheet_directory_uri() . '/js/landing-page.js', null);
            wp_dequeue_style( 'layout', get_template_directory_uri().'/css/layout.css', '', null );
        }
    }
}
add_action('wp_enqueue_scripts', 'scripts_and_css');

If I then change this to use the template name, it completely fails and doesn't load or remove any of the scripts or stylesheets.

My page template filename called page-landing-page.php:

<?php
/**
 * Template Name: Landing Page
 * The template for displaying the content for the landing pages.
?>
<?php wp_head(); ?>

// Got all my content loading in here.

<?php wp_footer(); ?>

Here's an example of what I've tried up to now in the functions.php fle:

if(is_page_template('Landing Page'))
{
    // Enqueue / Dequeue scripts / styles
}

--

if(is_page_template('page-landing-page.php')) // This is the name of my page template
{
    // Enqueue / Dequeue scripts / styles
}

--

if(is_page_template('landing-page.php')) // This is the name of my page template
{
    // Enqueue / Dequeue scripts / styles
}

--

if(is_page_template('landing-page')) // This is the name of my page template
{
    // Enqueue / Dequeue scripts / styles
}

Just cannot seem to get it to work. Any guidance would be appreciated!

For some reason, if you do not select the page template from the Template Dropdown on the edit page, is_page_template('template-name.php') doesn't seem to work.

I have found a kind-of-a-hacked solution to your problem. It seems to be working for both of the cases. Either you select the page template from the dropdown or the template gets selected by the page-slug.

if( basename( get_page_template() ) == 'page-price-watch.php' ) 
{
    // Enqueue / Dequeue scripts / styles
}

Thanks.

"is_page_template" works by checking the post meta. If the template is automatically pulled, for example because it's called home.php, the template being used is not filled into the meta. Meta is only filled when actively selecting the template for a page in the editor.

These always work and do not rely on the meta:

function your_enqueue_styles() {

    if (is_front_page()) {
        //works
    }

    if (is_page( array('pageslug1', 'pageslug2'))) {
        //works
    }

    global $template;
    if (basename($template) === 'template-name.php') {
        //works
    }

}
add_action( 'wp_enqueue_scripts', 'your_enqueue_styles' );

Try something like this to display the currently used page template at the bottom of the page when viewed as an admin, makes it easier to troubleshoot:

// Page template finder
function show_template() {
    if( is_super_admin() ){
        global $template;
        $output = '<div style="position: absolute; bottom: 0; width: 100%; text-align: center; z-index: 100; background-color: white; color: black;">';
        ob_start();
        print_r($template);
        $output .= ob_get_clean().'</div>';
        echo $output;
    }
}
add_action('wp_footer', 'show_template');