php - 使用 wp_read_audio_metadata()

标签 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>

最佳答案

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

作为记录,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>";

相关:Save camera info as metadata on image upload?

关于php - 使用 wp_read_audio_metadata(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43336867/

相关文章:

php - Docker 服务 php-fpm 容器之间的 cURL 导致 "Connection refused"

php - 如何让 Wordpress 以 Markdown 格式保存评论?

javascript - Onresize 事件未在调整大小时运行

javascript - JQuery Ajax动态表单,如何在表单上方的div中显示成功消息

php - CakePHP - 构建复杂查询

javascript - 有人可以帮我解决这个错误吗?

wordpress - 在 WordPress 的页面上显示多个类别帖子

css - 带有反应的 headless WordPress - 后端和前端 Gutenberg 的样式

php - 如何减少标题中的主页链接大小?

php - jquery中如何比较两个日期