2

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.

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.

2 Answers 2

7

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.

0

If you can find the post id within functions.php, you should be able to use

if (has_post_format($ID) )

no?

3
  • yeah I know that and I'm using it but I want to enqueue the script on the homepage and archive pages and search pages and every page that uses this post format, and there will be other many pages without these posts so I don't want to enqueue the scripts on all pages, and thanks for your reply.
    – Pierre
    Jun 19, 2012 at 17:47
  • on your homepage there may be several posts and may be both audio and video. so what is the purpose of identifying the type as there is high chances of mixed types? Jun 19, 2012 at 17:54
  • can you do a wp_query and do the condition within that, iterating over all the posts for a archive/home/search page? Jun 19, 2012 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.