Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nootrope/b48ff692f41cf5967e22a8aa588c032d to your computer and use it in GitHub Desktop.
Save nootrope/b48ff692f41cf5967e22a8aa588c032d to your computer and use it in GitHub Desktop.
WordPress enqueue styles by template
<?php
/***
From: https://developer.wordpress.org/reference/functions/wp_enqueue_style/
This is a conditional loading of css file by page template (css will be loaded on on the pages with template-name.php).
You can change the condition to others. The code should be used in your theme’s function.php.
Notice: The code works for child themes. If you want to use it in a parent theme replace get_stylesheet_directory_uri() with get_stylesheet_uri().
**/
$handle = 'wpdocs';
wp_register_style( $handle, get_stylesheet_directory_uri().'/relative/path/to/stylesheet.css', array(), '', true );
if ( is_page_template( 'template-name.php' ) ) {
wp_enqueue_style( $handle );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment