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
- Enqueue All Stylesheets Found In a Theme Folder
- Add custom action to the page listing screen
- Adding in additional pages into a custom theme
- My edits are not saved
- How to list pages from an author?
- Editing Pages Icons Are Not Showing Up – Why is this happening?
- Load script and styles using an array of resources
- How to sorted Parent Page in WP-admin
- Unable to defer loading of jquery
- Display meta data attribute in user profile
- How do I ensure I can loop through every enqueued script and CSS?
- Add the same content to multiple pages
- wp_list_pages get the hole hierarchy for current page
- Pages in wp-admin is not found 404
- Shortcode runs when editing page
- how can i display children of my parent page in one of the children page (wordpress)
- Cannot Create A Page or Post in WordPress 5.2.1
- My English homepage will not update
- How to avoid conflict if a plugin already have included open library?
- How to select what posts goes into which custom page template?
- Set different css stylesheet for specific pages [duplicate]
- Scripts and stylesheets failing to load in Chrome IOS (and only Chrome IOS)
- Displaying all pages which comes under a category id
- Enqueue script with version number not working
- Some pages can’t be updated – post_content field not updating
- How to enqueue a script on a specific URL that contains multiple parts
- Can I use the wordpress loop in pages?
- Add addtional page parameter before loading the page
- My Child Theme CSS Isn’t Working
- Problem with deleting a “Page”
- Can’t Workout Dependency of Stylesheets
- Static Home Page resets to Front Page
- How to add custom php file to right sidebar?
- WordPress and Advanced Custom Fields and Flickity
- Assign Parent Page – Now 404 Error
- Recovering a page from the drafts
- How to include a query_vars value in document_title_parts?
- Why my home page doesnt work?
- Deleted WordPress page appears in Google Search Results
- Create multiple custom HTML pages
- WordPress redirects to login page when I enter url.com/index.php