wordpress - Meta Data Not showing in Custom Shortcode Function -
i've created custom post type called sermons , added meta box post type insert mp3 url each post. mp3 url works/displays fine on sermons page template player not appear when create shortcode recent sermons in functions file. of other elements appear in loop (title, featured image, date) no audio player.
add_shortcode( 'recent_sermons', 'recent_sermons' ); function recent_sermons( $atts ) { ob_start(); // define attributes , 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 loop based on 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 work inside output buffer (ob_*
functions). global $post
testing, in code it's defined in loop.
add_shortcode( 'test-wp-embed', function( $atts, $content ) { global $post, $wp_embed; ob_start(); $mp3 = get_post_meta( $post->id, 'wpshed_textfield', true ); echo do_shortcode( $wp_embed->autoembed( $mp3 ) ); $myvariable = ob_get_clean(); return $myvariable; });
but i'd suggest changing code use get_posts
instead of wp_query
. in case , without output buffering, works:
add_shortcode( 'test-wp-oembed', function( $atts, $content ) { global $post; $mp3 = apply_filters( 'the_content', get_post_meta($post->id, 'wpshed_textfield', true ) ); return $mp3; });
Comments
Post a Comment