I created multiple post formats, and when i use an audio post format, i want to include some additional javascript for the audio player.
So, i include the content-audio.php like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blog/content', get_post_format() ); ?>
<?php endwhile; ?>
This loads the blog/content-audio.php as expected:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
$att_args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type' => 'audio',
'orderby' => 'menu_order'
);
$attachments = get_posts( $att_args );
if( $attachments ): ?>
<?php wp_enqueue_script('mediaelement-js'); ?>
<?php foreach( $attachments as $attachment ): ?>
<?php $attachmenturl=wp_get_attachment_url($attachment->ID); ?>
<audio id="player2" src="<?php echo $attachmenturl; ?>" type="audio/mp3" controls="controls"></audio>
<?php endforeach; ?>
<?php endif; ?>
<?php the_title(); ?>
</article>
Can i use the wp_enqueue_script() function inside this file? It’s working fine, but i’m curious if this is the right way to do it.
As of WordPress 3.4, calls to
wp_enqueue_script()
can be made inline. So, it is perfectly acceptable to call it where needed, such as inside a shortcode callback, or conditionally based upon returned queries, as you’ve done here.