Inside your theme’s functions.php
add something like this:
function my_conditional_enqueue_script() {
global $post;
if (is_single($post->ID) && in_category('mouse', $post->ID)) {
wp_enqueue_script('mousescript');
}
}
add_action('wp_enqueue_scripts', 'my_conditional_enqueue_script');
Also, make sure you use wp_register_script
before you attempt to enqueue.
Related Posts:
- Using get_option() in JavaScript
- WP_enqueue_script inside shortcode?
- Enqueue Scripts / Styles when shortcode is present
- Conditionally Loading JavaScript/CSS for Shortcodes
- How to add stylesheets only to pages with specific shortcode?
- Tinymce – How to hook before or after live shortcodes rendering?
- How execute shortcode with javascript
- Including javascript for a shortcode
- Shortcode to Gutenberg-block: additional text on front-end and conditional display
- Can’t get JS code to work with shortcode
- How resource intensive is wp_register_script()?
- How to handle shortcodes when using the JSON API
- Pass javascript array to shortcode
- Add custom setting that uses radio button to WP Gallery
- Convert HTML Script to Shortcode [closed]
- Get Required Assets (JS, CSS etc) for post using REST API
- How to force that styles are enqueued in the header?
- Scripts for shortcode: concatenate or load separately? [closed]
- Adding javascript actions to words in post content
- Conditionally Loading JavaScript/CSS for Shortcodes
- Adding JS in header when using wp_enqueue_script in a shortcode
- Add JS in footer via shortcode?
- Deactivate JS Script in Plugin Shortcode
- enqueue script with get_shortcode_regex
- Regenerating WordPress shortcodes based on value with AJAX
- WordPress Shortcode / Only works in classic editor, gutenberg error / invalid json response
- wp_enqueue_scripts does not work on admin page
- Displaying an interactive web map on my wordpress site
- Multiple shortcodes with embedded JavaScript code in same page, how?
- Simulate shortcodes with javascript
- Clarify: Enqueueing scripts within shortcode only loads in footer?
- wp_register_script and wp_register_style when shortcode is used
- Why does my JavaScript work in the browser console but does not work in my shortcode?
- Trying to create a shortcode that displays taxonomy terms in a dropdown
- do_shortcode within a shortcode
- tiny_mce_before_init preventing custom shortcode icon appearing
- Custom Fields – Boolean Display of True/False to Yes/No
- How to enqueue some javascript source once per page
- Linking wp_enqueue can’t find the javascript file (adds “?ver=x.x.x” to the src)
- Which file renders the “Edit Gallery” Settings page?
- Failing to load my script files in wordpress! i can’t figure out what i’m doing wrong
- using shortcodes in theme content
- How to: Simple Nested Shortcode [closed]
- Insert First Image into post (full-width)
- How to display NextGEN gallery in templates?
- Clearfix Shortcode
- How and when to include shortcode source files
- Use full size images in a specific gallery
- Excerpt Length & List Category Posts Plugin
- Iteratively add sub shortcodes to php array
- How To Include JavaScript Files Only In Single Posts?
- Setting Variable Path to Template Directory inside Script
- wp_enqueue_script does not recognize my js file?
- Twitter bootstrap tabs
- Plugin renders in preview but shortcode shows when published
- How to process shortcode on post save and update only
- Shortcode not displaying video as expected
- contact form 7 database short codes [closed]
- jQuery Tabs in Shortcode API
- Enclosing Shortcode not working in a Template
- Show shortcode only in posts
- how to create a fav icon shortcode?
- How to deal with WordPress bug: can’t use wrapped and unwrapped shortcode on same post
- How to execute a shortcode?
- How to add a shortcode that creates an adjacent column of text in the content editor?
- get_posts not working on homepage?
- wp_enqueue_script() not working
- Execute Shortcodes when submitting post
- Substituting value via shortcode
- Shortcode, Concatenating & Parse error: syntax error, unexpected T_FOREACH
- What to include to use jQuery UI Auto Complete
- how to write short code in word press
- Better way to display multiple plugin output on the same page?
- hard code shortcode only on specific category
- Dynamic number within shortcode
- How to turn this HTML code into a shortcode? (adding song lyrics and giving an id to a div)
- get_queried_object_id / short code returning blank
- Bootstrap bundle present in page source but not working
- Video shortcode, option are not rendered
- ACF repeater + group shortcode
- Custom shortcode skips every 2nd shortcode
- wp_enqueue_script JS code runs too late (after user begins interacting)
- Create shortcode for each form field in contact form 7
- Build Clickable Link Using Multiple Attributes
- Set redirect for need login
- Getting Shortcode Attributes befor content is displayed by the template
- Get localize of a loaded javascript
- make user_id in a shortcode dynamic based on who’s logged in
- How to add site tags in alphabetical order to shortcode?
- Dynamically add Js
- JS / jQuery in Elementor pages vs JS file
- Load JavaScript on specific page with @wordpress compiler
- How can i strip shortcode from HTML title tag?
- Can’t render shortcode when html tag contains hyphen
- Adding shortcode of “Random Content” Plugin in the post after the title
- Shortcode won’t take into account custom post ID put in parameter
- using shortcodes inside gravity forms
- WordPress load same script two times
- How to load Javascript code or functions.php later in a child theme?
- wordpress script_loader_tag in function.php