2014-03-24 21 views
1

我創建了一個自定義帖子類型,稱爲佈道,併爲此帖子類型添加了一個元框以爲每個帖子插入MP3網址。 MP3 URL在我的講道頁面模板上正常工作/顯示,但是當我在我的功能文件中爲近期講道創建簡碼時,播放器不會出現。所有其他元素都出現在循環中(標題,特色圖片,日期),但沒有音頻播放器。在自定義短代碼功能中未顯示的元數據

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; 
    } 
} 

回答

1

以下內容將在輸出緩衝區內工作(ob_*函數)。 global $post只是用於測試,在您的代碼中它是在循環中定義的。

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; 
}); 

但我建議你改變你的代碼使用get_posts instead of WP_Query。在這種情況下,沒有輸出緩衝,這可以工作:

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; 
}); 
+0

對不起Brasofilo,但我不明白我需要在我的代碼中添加此項。 – Breon

+0

@Breon,把'$ mp3 = get_post_meta($ post-> ID,'wpshed_textfield',true); echo do_shortcode($ wp_embed-> autoembed($ mp3));'而不是你的'<?php echo apply_filters('the_content',ETC ...?>'。 – brasofilo