top-arrow

How To Use wp_enqueue_script or wp_enqueue_style For Specific Page, Post or Post Type

wp_enqueue_script

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 it’s the right article for you. I’ve discussed below how to enqueue a file for a specific page or post in a WordPress site. So let’s 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
// let’s 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

// let’s 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

// let’s 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
// let’s 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
// let’s 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

// let’s 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:

2 comments

6 October, 201911:11 am

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

Leave a Reply

Mohammad Rahat
about me

Mohammad Rahat Tanjid

Mohammad Tanjid is a professional WordPress Developer and Designer, he builds WP Products like WP Themes/Plugins and author of webextent.net. He has been writing code for more than 5 years now.