admin管理员组

文章数量:1326285

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' );

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' );

Share Improve this question asked Aug 4, 2020 at 18:21 التقنية techالتقنية tech 133 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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' 
}

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( ... ) ; 
}

本文标签: pluginsHow to make wpenqueuestyle and wpenqueuescript work only on custom post type