Conditionally enqueue a script if a custom post type exists
I am trying to enqueue a .js script on my blog home page only if a custom post type exists. I have registered a custom post type ‘featured_post’ which will be inserted into a slider. I can conditionally add the slider and html for ‘featured_post’ to the home page inside the loop using
post_type_exists( 'featured-post' )but I cannot get the .js file to load conditionally if we are on home and there are any ‘featured-post’ posts.
I had this working and loading the script conditionally using sticky posts but I cannot get it to work with a custom post type. It will load the script if I use
post_type_exists( 'featured-post' )but in that case it will still load it if there are no ‘featured post’ posts as the post type is registered regardless of whether it contains posts or not.
Any help would be much appreciated.
- The topic ‘Conditionally enqueue a script if a custom post type exists’ is closed to new replies.