How do I embed styling that will affect this template only?
The trick is to hook into template_include
. This allows us to see which template is being applied to a page.
Suppose we have a template with a basename of ‘full-width.php’. Inside our template_include callback, we can conditionally enqueue a CSS style only when the template is full width.
function wpse_258048_template_include( $template ) {
if( 'full-width.php' === basename( $template ) ) {
wp_enqueue_style( 'full-width', PATH_TO . '/full-width.css' );
}
return $template;
}
add_action( 'template_include', 'wpse_258048_template_include', 1000, 1 );
Related Posts:
- How to change background color in the Notepad++ text editor?
- What are all the available parameters for query_posts?
- Does WordPress work without a theme?
- Link to specific Customizer section
- When cropping a header image, retain meta data (i.e. name, description, etc.) from original image?
- How to create a new theme from scratch?
- Difference between stylesheet_directory and template_directory
- Adding a Template to the Page Attributes Panel for both Posts and Pages?
- How do you change the theme location?
- How to remove “Proudly powered by WordPress” in Twenty Sixteen (2016) theme?
- How do I put my comment form above the comments?
- How can I check for malware in a Theme? [closed]
- Edit theme wp_head
- Theme Advanced Styles in Visual Editor and Paragraphs
- Want to have the Post editor remembering the last editing position
- Enqueue different stylesheets using IE conditionals
- Override parent theme translation on child theme
- Forbidden 403 error, permission to access, unable to read htaccess
- Theme of codinghorror.com
- How to add template to theme in WP
- How do I copy my Parent Templates to my Child Templates?
- How can I show more than 15 themes at a time in the admin menu?
- What is the WordPress Pinecone sitting on top of? [closed]
- Download WordPress.com theme: how?
- Create a dashboard-style site (not WordPress dashboard) – is there a theme? [closed]
- Help with WordPress Query
- How do I set the featured image size on the single post?
- Are theme .php files stored in the database?
- Inject widgets from one sidebar into another with PHP
- Execute filter in theme as last
- Why style.css with ?ver=3.9.2 not updating as the one without ?ver=3.9.2?
- An unexpected error occurred. Something may be wrong with WordPress.org
- Inconsistent translation on a WP site
- How can I have different theme with same booking table? [closed]
- Updating my free theme delete all my changes [closed]
- Troubles with Walker_Nav_Menu and closing
- Different logo on homepage
- Translation not working after editing plugin template
- How to apply different themes with custom design in one site?
- How to make Additional css changes global all across the web pages on WordPress website?
- WordPress without a theme – help!
- How to restore file or theme?
- A /proc folder in wp-content?
- how do i in ignore/disable/delete a javascript call from parent in child theme?
- Problem Installing a Premium Theme– File Permission Issue on a Mac, Local Machine? [closed]
- Child theme’s stylesheet cannot load [closed]
- Is it good practice moving the main stylesheets after the plugin styles?
- Variant of the same theme for each page
- can’t see aditional themes in control panel
- When someone likes an article on my blog, they are prompted to log in on WordPress.com [closed]
- WordPress TwentyTen Theme Incompatible with Theme Unit Test
- What’s the default order used by WordPress to load CSS files?
- WordPress theme logo handling (Trying to use GIF as logo)
- How can I remove all traces of a theme?
- Changing an image in header.php
- Installing a theme on localhost, Ubuntu 16.04
- Unable to find translations in WordPress theme
- A thin line between native wordpress bahaviour and 3rd party themes
- Using filter in the widget causes fatal error
- Display Pointer only once?
- Will I lose the pages I’ve created within a default WordPress theme when adding a new theme?
- Theme title in admin shows full path [closed]
- Is there a way to see how a theme will look before buying it?
- Menu items do not “open” when mouse hover over them?
- From where wp ecommerce is loading plugin theme files?
- Making first post wider than other posts on home page(default)
- Twenty Ten Theme: Replace the Logo by a picture without moving the code
- In a specific blog, the K2 theme does not have a Text Widget
- How to push a theme update for a ciustom theme used on multiple independant self hosted sites?
- Removing a widget via theme editor
- theme customizations gone after enabling ssl
- Can’t embed certain youtube videos into theme
- How to customize a section of a widget only on certain pages or page
- How do I get the featured image to sit below the header in the twenty nineteen theme?
- Simple filter to change label name of Email Adress to something else
- Basic wordpress search shows no results
- How do I remove search bar that isn’t a widget?
- Show only the beginning of post
- Changing theme folder name disrupts widgets
- WordPress Customazation API section in section
- TwentyFourteen Theme : Changing grid layout
- How can I Update my Theme
- Use random theme
- Replacing current theme folder by previous version of the same folder produces “undefined function get_header()” error
- Theme comment callback results nothing
- Does not work bookmarklet in the theme Tagology
- How do I block a subdirectory from WordPress theming?
- How to remove the option data of a theme when that theme is removed?
- Resetting Themes Folder to Default
- What is the general cut-off date for reviewed themes in the WordPress.org repository?
- Sub-Theme (based on Taxonomies)
- How to Hide Blog Post Author?
- Commenting in CSS (//) equals “none”? [closed]
- Adding a wp_head hook from an included PHP file
- Deleting Widgets in sidebar in Panthea theme
- Is it possible to dynamically show different themes for different users? [duplicate]
- Why does WP theme not look like promoted? [closed]
- Remove image next to header image on WellExpo theme [closed]
- How do I get rid of the Mindblown and the book recommendations?
- Adding PHP code to single template in 2023 edition of WordPress theme