It really needs sometimes to use wp_enqueue_script or wp_enqueue_style for specific page or post. If you would like to enqueue a style or script in your WordPress site then its the right article for you. Ive discussed below how to enqueue a file for a specific page or post in a WordPress site. So lets get started.
This step I’ll follow for making this post more useful for you.
How to enqueue a file for a specific page.
// use of wp_enqueue_style() for specific page // lets check we are in the specific page // we can use page ID or page Slug function webextent_enqueue_styles() { if ( is_page( 33 ) ) { wp_enqueue_style( custom-stylesheet, get_template_directory_uri() . '/style.css' ); } } add_action( 'wp_enqueue_scripts', 'webextent_enqueue_styles' );
use of wp_enqueue_script() for a specific page
// lets check we are in the right page // we can use page ID or Slug to check function webextent_name_scripts() { if ( is_page( 34 ) { wp_enqueue_script( 'webextent-custom-script', get_template_directory_uri() . '/js/webextent-custom.js', array(), '1.0.0', true ); } } add_action( 'wp_enqueue_scripts', 'webextent_name_scripts' );
If you don’t know how to collect a post ID, you visit how to add body class in a WordPress site where I’ve shown how to collect post ID or page ID.
Now I would like to discuss how to enqueue file for specific post
How to enqueue a file for a specific post
// lets check we are in the specific post // we can use page ID or post title function webextent_enqueue_styles() { if ( is_single( 18 ) ) { wp_enqueue_style( custom-stylesheet, get_template_directory_uri() . '/style.css' ); } if ( is_single( Hello world ) { // this will work where post title is Hello world wp_enqueue_style( custom-stylesheet, get_template_directory_uri() . '/style.css' ); } } add_action( 'wp_enqueue_scripts', 'webextent_enqueue_styles' );
use of wp_enqueue_script() for specific post
// lets check we are in the right post // we can use Post ID or post title function webextent_scripts() { if ( is_page( 34 ) { wp_enqueue_script( 'webextent-custom-script', get_template_directory_uri() . '/js/webextent-custom.js', array(), '1.0.0', true ); } } add_action( 'wp_enqueue_scripts', 'webextent_scripts' );
How to enqueue a file for the specific post type
// use of wp_enqueue_script() for specific post type // lets check we are in the right post type // we can use post type slug for that. function webextent_scripts() { if ( get_post_type( get_the_ID() ) == post-type-slug ){ wp_enqueue_script( 'webextent-custom-script', get_template_directory_uri() . '/js/webextent-custom.js', array(), '1.0.0', true ); } } add_action( 'wp_enqueue_scripts', 'webextent_scripts' );
use of wp_enqueue_style() for specific post type
// lets check we are in the right post type // we can use post type slug for that. function webextent_enqueue_styles() { if ( get_post_type( get_the_ID() ) == custom-post-type-slug ) { wp_enqueue_style( custom-stylesheet, get_template_directory_uri() . '/style.css' ); } } add_action( 'wp_enqueue_scripts', 'webextent_enqueue_styles' );
Note: You can follow this method for a specific tag or category.
Tags: enqueue file WordPress How to enqueue file for specific page how to enqueue file for specific post type wordpress WordPress support wp_enqueue_script
2 comments
admin168
thank you for the page and also the article that you have made is very useful for me and many people who need it.
greetings from my page writer
mdtanjid
Glad that it helps you!