Wordpress Hooks

How to add Enqueue Script and Style for Single page, Archive Page and Page Templates?

Enqueue Script and Style for Single page, Archive Page and Page Templates

wordpress
206Views

Hello, welcome to therichpost.com. In this post, I will tell you, How to add Enqueue Script and Style for Single page,  Archive Page and Page Templates? WordPress is the best cms. WordPress hooks(add_action, add_filter) give us the power to edit or change the code without interruption into the files and this is the best thing about wordpress. Now I am going to tell you how the hooks work.

In this post,  we will add conditional scripts or styles for particular page template,  
custom post type single pages or archive pages and this is very useful.
Here is the working wordpress hook  for How to add Enqueue Script and Style for Single page,  Archive Page and Page Templates, and you need to add this into your theme’s functions.php file:
add_action( 'wp_enqueue_scripts', 'so_50916971_enqueue_scripts' );
function so_50916971_enqueue_scripts(){
    /**For single of archive page */
    if( is_singular( 'movies' ) || is_post_type_archive( 'movies' ) ){
        wp_enqueue_style( 'my-movie-style', /* src to .css file */ );
        wp_enqueue_script( 'my-movie-script', /* src to .js file */ );
    }

   /** For page templates */
   if ( is_page( 'landing-page-template-one' ) ) {
    //you style
    } 
}

 Now you are done and if you have query related to this post or you want to do some more with this code then please do comment below and I will come with wordpress hooks.

 

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL