Yes, you may add conditional tags to the wp_enqueue_scripts
action. See the examples below:
function load_assets() {
wp_enqueue_style( 'styles', get_template_directory_uri() . '/css/styles.css');
// loads on any 'page' post type
if( is_page() ){
wp_enqueue_script('main-js', get_template_directory_uri() . '/js/main.js');
}
// only loads on the page with a slug of 'home'
if( is_page('home') ){
wp_enqueue_script('home-js', get_template_directory_uri() . '/js/home.js');
}
}
add_action( 'wp_enqueue_scripts', 'load_assets' );
For other examples of is_page()
usage, see the codex page:
https://codex.wordpress.org/Function_Reference/is_page
Related Posts:
- How can I get a list of all enqueued scripts and styles?
- Dynamically Load Styles and Scripts from Theme Functions.php
- Add external css to Contact Page
- Enqueuing a script before anything else
- is there a way to get all queued scripts/styles into a template without `get_header()`?
- Adding styles/scripts to specific page
- How to load scripts/styles specific for a page
- How to add stylesheets only to pages with specific shortcode?
- How to load JS and CSS only on specific Pages using is_page()?
- Get list of scripts / styles and show file which enqueued them
- Why I can’t add a CSS style in this WordPress theme?
- How can I dequeue a Plugin Stylesheet?
- Use wp_enqueue_scripts, not wp_print_styles?
- How to combine multiple CSS files and concatenate JavaScripts if WordPress recommends enqueuing them?
- What are the options for running custom css and javascript files on a page?
- Enqueued Stylesheets Effecting Admin Styles
- wordpress with many custom styled static pages
- Relative path instead of absolute for wp_enqueue_script
- How to load script-related styles automatically?
- Custom Admin CSS styles to style media uploader?
- How to avoid loading same script twice?
- How to insert html/css/javascript code to wordpress plugin
- Styles and scripts inside template part
- External stylesheet per page
- Child theme stylesheet not always overriding “index”
- wp_enqueue_script was called incorrectly Issue when active plugins of WordPress Admin Bar Improved
- Adding a Calculator to a WordPress Page
- Prioritize visible content – Page speed issue on Google insights
- Scripts not loading when using the wp_enqueue_scripts action
- Custom Plugin – CSS works, JS doesn’t
- Change a Page’s Header Image
- Styling Previous/Next Pages differently from Index
- How can I force Dynamic CSS via ACF values to update on page load?
- functions.php – inject inline css from file
- enqueing Javascript and CSS
- Load custom CSS before admin CSS
- Theme JS is available but theme CSS isn’t
- Including CSS files in HEAD based on page content
- Different css file not working for the page template I created
- I want to set global directory locations for my CSS and JS locations. How?
- Multiple Loops That Pull Pages (Child/Grandchild) While Styling the First Entry
- Advanced method to control cache of enqueued style/script
- Dequeue styles with query doesn’t work
- Bootstrap Integtration
- Why can I not deregister my style and script on custom post type?
- Set different heights per screen size with inline styles CSS into the ‘Text’ section of a Page
- WordPress default theme cannot get full page on entry content div
- How can a .css file be applied to a virtual page?
- How do I dequeue a Stylesheet, stored in an ‘Assets’ folder?
- Converting HTML to WordPress theme: integrating pages function and using its text editor, conditional enqueuing
- Blank white space where header was [closed]
- Adding CSS styles to Admin Area PAGES only (not POSTS or CPT)
- CSS Styling for a Page/ Site made with Pagelines
- How to align bold,italic and underline in wp-admin?
- Bootstrap 4 Optimization
- Edit the text location of a form
- plugin not loading my css and js
- custom css in one page
- featured image as background image on pages
- Can I display submenus in groups, using wp_list_pages?
- Can I display the widget admin in the page admin?
- Different “Text Widgets” in Sidebar on Many Different Pages?
- wp_enqueue_style specifics for overwriting plugin styles
- If is multiple page templates
- How to echo excerpts with wp_list_pages?
- Remove pages from backend list *and* update counter accordingly
- Never Enqueued Stylehsheet
- Changes in enqueued / registered stylesheet paths not updating—why?
- Loading different stylesheet on WordPress pages?
- Making a wordpress page the index on the site?
- Using Pages to handle calls for custom post types
- Displaying Subpages while on Parent page?
- Guest authors, sort of like Wikipedia, but more theme-able
- Pages views limit [closed]
- Having wordpress page accesss issue under sub-directory
- How can I properly enqueue tags in WordPress to use with Web Components?
- How to make my category archive behave like a page
- how to edit source code of specific page in wordpress?
- Dynamic href link to Contact Page
- Restrict the list of parent pages to only those which are created by current user
- Is there a way to schedule changes to a page?
- How to properly add my styles overriding current styles
- Option for pages order in backend
- Return subpages of an author if the parent page was published by an other author
- Setting Variable Path to Template Directory inside Script
- Dynamically add Js
- Next and Previous Page links based on ‘Order’ attribute
- Update Pages ONLY from Stage to Live (not products)
- Changing the CSS with a plugin
- Every change made by “Author” role is going to “pending review”
- Create multiple custom HTML pages
- Add homepage to the list of pages in the Footer Widget
- wordpress doesnt add media.css file
- How to turn post into pages and via versa?
- Featured Image not displaying in a page
- simple category search form returns full text – but I need excerpts
- Naming custom page templates
- In WP, Google map error initMap is not a function [closed]
- Fresh WordPress installation not able to simply show page content