How to use wp_enqueue_style() and wp_enqueue_script() only when needed?

I have not tested it, but could is_main_query() (appeared in 3.3) be used here to test that the query being run is the query used to populate ‘the Loop’. (I’m not sure how it handles new WP_Query instances, though).

The Codex says that it can be used on the pre_get_posts, but looking at the code I can’t see why it can’t be called on a later hook (i.e. the_posts)- all it does is check the global $wp_the_query against the current query.

Leave a Comment