使用 wp_read_audio_metadata() [英] Using wp_read_audio_metadata()
问题描述
我正在尝试从 WordPress 的 mp3 文件中获取一些元数据.特别是长度变量.这是我的一些代码.此处未显示,但我已包含 wp-admin/includes/media.php 文件.当我查看我的页面 http://beta.openskyministry.org/podcasts/ 时,我只看到空白
I am trying to get some metadata from an mp3 file in WordPress. Specifically the length variable. Here is a bit of my code. It's not shown here but I have included the wp-admin/includes/media.php file. When I look at my page http://beta.openskyministry.org/podcasts/ I just see empty tags for <itunes:length></itunes:length>
如果您需要其他帮助来回答我的问题,请告诉我.
Let me know If you need anything else to help answer my question.
$aud_meta = wp_read_audio_metadata($aud_url); ?>
<item>
<title><?php the_title(); ?></title>
<itunes:author><?php echo htmlspecialchars((get_bloginfo('name'))); ?></itunes:author>
<itunes:summary><?php echo htmlspecialchars(strip_tags(get_the_excerpt())); ?></itunes:summary>
<itunes:length><?php echo $aud_meta['length_formatted']; ?></itunes:length>
推荐答案
WordPress 已经存储了媒体元数据,因此无需再赘述.解决方法很简单:
WordPress already stores media metadata, so there's no need to go over it. Solution is as simple as:
add_action( 'wp_head', function(){
global $post;
if ( is_single($post) ) {
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_parent' => $post->ID,
'post_mime_type' => 'audio'
);
$attachments = get_posts( $args );
if($attachments){
$meta = wp_get_attachment_metadata( $attachments[0]->ID );
echo "<itunes:length>{$meta['length_formatted']}</itunes:length>";
}
}
});
对于记录,wp_read_audio_metadata()
需要文件路径,而不是 URL.如果需要,应该是:
For the records, wp_read_audio_metadata()
expects the file path, not the URL. If needed, it should be:
$path = get_attached_file( $attachment->ID );
$meta = wp_read_audio_metadata($path);
echo "<itunes:length>{$meta['length_formatted']}</itunes:length>";
这篇关于使用 wp_read_audio_metadata()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!