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’ );
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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'
}
Method 2
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( ... ) ;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0