0

Could anyone tell me what is the best and most efficient way to add separate style sheet only to one template? Do I have to have separate header and call it there or there is some Wordpress trick?

1

If you are talking about a page template, then you can do a conditional check with is_page_template() and includethe CSS in the header with wp_enqueue_syle(). Something like this:

function themename_include_page_specific_css() {
    if ( is_page_template( 'template-name.php' ) :
        wp_enqueue_style(
            'themename_page_specific_css',
            get_template_directory_uri() . '/page-specific-style.css'
        );
    endif;
}
add_action( 'wp_enqueue_scripts', 'themename_include_page_specific_css' );

Keep in mind, that the parameter you pass to is_page_template() must be the name of the template including the path relative to the directory of the theme. Meaning that if your template is in templates folder you should write it like:

is_page_template( 'templates/template-name.php' )

This will also work with other conditional tags like is_page(), is_single(), and similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.