I’ve created a custom post type called sermons and added a meta box to this post type to insert a MP3 URL for each post. The MP3 URL works/displays fine on my sermons page template but the player does not appear when I create a shortcode for Recent Sermons in my functions file. All of the other elements appear in the loop (title, featured image, date) but no audio player.
add_shortcode( 'recent_sermons', 'recent_sermons' );
function recent_sermons( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'posts' => 8,
), $atts ) );
// define query parameters based on attributes
$options = array(
'posts_per_page' => $posts,
'post_type' => 'sermons',
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) { ?>
<div class="my-sermon-loop">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="sermon rec-post-wrap">
<div class="sermon-post">
<div class="float-lft">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('%s', 'everypraise'), the_title_attribute('echo=0')); ?>"><?php the_title(); ?></a></h2>
<div class="loop-post-meta">
<ul>
<li class="date"><?php the_time(__('F jS, Y', 'everypraise')) ?></li>
</ul>
</div>
</div>
<div class="float-rt"> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('%s', 'everypraise'), the_title_attribute('echo=0')); ?>"><?php the_post_thumbnail( 'sermons-thumb' ); ?></a> </div>
<div class="cleared"></div>
<div class="sermon-audio">
<?php echo apply_filters('the_content', get_post_meta($post->ID, 'wpshed_textfield', true)); ?>
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
The following will work inside the Output Buffer (
ob_*
functions). Theglobal $post
is just for testing, in your code it’s defined in the loop.But I’d suggest changing your code to use
get_posts
instead ofWP_Query
. In that case and without the output buffering, this works: