1

How to make wp_enqueue_style and wp_enqueue_script work only on custom post type

I have install plugin on my wordpress that creat custom post type which is "http://localhost/wordpress/manga" and i have other custom post type like "http://localhost/wordpress/anime" so i only want to css work on manga not anime or in the front page

this is the code: wp_enqueue_style( 'wp-manga-plugin-css', WP_MANGA_URI . 'assets/css/style.css' );

2 Answers 2

1

You can use conditionals:

<?php
add_action( 'wp_enqueue_scripts', 'wpse_enqueues' );
function wpse_enqueues() {
    // Only enqueue on specified single CPTs
    if( is_singular( array( 'anime', 'manga' ) ) ) {
        wp_enqueue_style( 'wp-manga-plugin-css', WP_MANGA_URI . 'assets/css/style.css' );
    }
}
?>

If you need the CSS on archives as well, that's another condition:

if( is_singular( array( 'anime', 'manga' ) ) || is_post_type_archive( array( 'anime, 'manga' ) ) ) {
    wp_enqueue_style( 'wp-manga-plugin-css', WP_MANGA_URI . 'assets/css/style.css' 
}
0

Perhaps you're looking for is_singular, which you could put in e.g. your theme or functions.php to test for your CPT like:

if (is_singular('anime')) {
   wp_enqueue_style( ... ) ; 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.