Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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

if (has_post_format($ID) )

no?

share|improve this answer
    
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. –  Peter Jun 19 '12 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? –  HungryCoder Jun 19 '12 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? –  Zach L Jun 19 '12 at 18:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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