使用 wp_read_audio_metadata()

Chr*_*tle 5 php wordpress

我正在尝试从 WordPress 的 mp3 文件中获取一些元数据。特别是长度变量。这是我的一些代码。此处未显示,但我已包含 wp-admin/includes/media.php 文件。当我查看我的页面http://beta.openskyministry.org/podcasts/我只看到空标签<itunes:length></itunes:length>

如果您需要其他任何帮助来回答我的问题,请告诉我。

$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>
Run Code Online (Sandbox Code Playgroud)

bra*_*ilo 3

WordPress 已经存储了媒体元数据,因此无需对其进行检查。解决方案很简单:

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>";
        }           
    }
});
Run Code Online (Sandbox Code Playgroud)

作为记录,wp_read_audio_metadata()需要文件路径,而不是 URL。如果需要的话,应该是:

$path = get_attached_file( $attachment->ID );
$meta = wp_read_audio_metadata($path);
echo "<itunes:length>{$meta['length_formatted']}</itunes:length>";
Run Code Online (Sandbox Code Playgroud)

相关将相机信息保存为图像上传的元数据?