How to enqueue scripts depending on post formats?

So I’m trying to enqueue scripts used for some post formats like video and audio and I could find a way to check what post formats are going to be shown on the page.

I tried print_r on some global variables but they doesn’t seems to have post_format in the post details, I tried $posts and $wp_query and also tried print_r on $GLOBALS.

Read More

I also tried adding an action for video and audio post formats but the action runs too late for queuing scripts.

have anyone done something like this before ?

and thanks in advance.

Related posts

Leave a Reply

2 comments

  1. Inside the Loop, try something like the following:

    <?php
    if ( get_post_format() && in_array( get_post_format(), array( 'audio', 'video' ) ) ) {
        // The current post has either the
        // audio or video post format;
        // enqueue a script
        wp_enqueue_script( $handle, $src );
    }
    ?>
    

    Now that WordPress can handle inline script-enqueueing, this should work fine.