Simply follow the link to the has_shortcode()
documentation in the last answer to the duplicate question. There you’ll find:
function custom_shortcode_scripts() { global $post; if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-shortcode') ) { wp_enqueue_script( 'custom-script'); } } add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');
For styles this would be:
function custom_shortcode_styles() {
global $post;
if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-shortcode') ) {
wp_enqueue_style('custom-shortcode-css', get_template_directory_uri() . '/css/custom-shortcode.css');
}
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_styles');
Replace custom-shortcode
inside has_shortcode()
with the shortcode you are actually looking for, then put that snippet in your functions.php
or in your plugin and code on!