To enqueue a script and style only on the single and archive pages of a custom post type, you’ll need to use conditional tags within your enqueue function in your theme’s functions.php file.
Here’s an example of how to enqueue a script and style only for the single and archive pages of a custom post type called my_custom_post_type:
- In your WordPress admin dashboard, go to “Appearance” > “Theme Editor.”
- In the right sidebar, locate and click on the “functions.php” file.
- Add the following code snippet at the end of the file:
function my_theme_enqueue_scripts_styles() { if (is_singular('my_custom_post_type') || is_post_type_archive('my_custom_post_type')) { // Enqueue your script wp_enqueue_script('my-custom-post-type-script', get_template_directory_uri() . '/js/my-custom-post-type-script.js', array('jquery'), '1.0', true); // Enqueue your style wp_enqueue_style('my-custom-post-type-style', get_template_directory_uri() . '/css/my-custom-post-type-style.css', array(), '1.0'); } } add_action('wp_enqueue_scripts', 'my_theme_enqueue_scripts_styles');
Replace my_custom_post_type with the actual name of your custom post type. Also, replace the file paths (e.g., /js/my-custom-post-type-script.js and /css/my-custom-post-type-style.css) with the correct paths to your script and style files in your theme.
This code snippet creates a function called my_theme_enqueue_scripts_styles() that checks if the current page is a single page or an archive page of your custom post type using is_singular() and is_post_type_archive() conditional tags. If the condition is met, the script and style are enqueued.
- Click “Update File” to save the changes.
Now, your script and style will be enqueued only on the single and archive pages of your custom post type.