如何从wordpress音频播放列表中获取个别歌曲?

时间:2017-11-23 04:30:47

标签: php wordpress audio playlist

我正在尝试从创建的播放列表中获取歌曲名称和网址。创建帖子时动态生成播放列表。我将不得不循环浏览这些帖子并获取我要分割的播放列表并显示名称,持续时间等。

我已搜索过,但我能找到的只是这些播放列表的插件。

我想从发布的播放列表中分割音频文件,并仅在我网站的某些部分显示歌曲的名称。

我可以将单个音频文件显示为

$music_file = get_template_directory_uri() . "/sounds/music.mp3"; 
echo do_shortcode('[audio mp3=' . $music_file . ']');

但我希望从播放列表中获取所有音频文件并单独循环播放它们并创建播放列表的自定义显示。

更新

例如,我可以使用$gallery = get_post_gallery_images( $post )从图库中获取单个图像。是否有一个功能可以为音频播放列表执行相同的操作。

播放列表是通过编辑器中的媒体上传创建的

enter image description here

3 个答案:

答案 0 :(得分:2)

虽然您的问题并不是很清楚,但此代码可以帮助您找到播放列表中的每个音频名称/网址:

// your playlist shortcode
$playlist_shortcode = '[playlist ids="2217,2578,2579"]';

// Find registered tag names in your $playlist_shortcode.
preg_match('/' . get_shortcode_regex() . '/', $playlist_shortcode, $match );

// Parse playlist shortcode attributes
$playlist_attr = shortcode_parse_atts($match[3]);

// Retrieve audio ids
$audio_id = explode(',',$playlist_attr['ids']);
foreach($audio_id as $id ){
    // Single audio title
    echo get_the_title($id);

    echo do_shortcode('[audio mp3=' . wp_get_attachment_url($id) . ']');
}

答案 1 :(得分:0)

wp_playlist_shortcode()的源代码中,您将找到所需的查询,可以将其提取并用于构建您自己的查询,如下所示。

<?php
$playlist_attachments = get_children( [
    'post_status'    => 'inherit',
    'post_parent'    => get_the_ID(),

    'post_type'      => 'attachment', // Attachment post type.
    'post_mime_type' => 'audio', // Audio file attachments.
    'orderby'        => 'menu_order ID', // Playlist order.
    'order'          => 'ASC', // Ascending order.
] );

foreach ( $playlist_attachments as $id => $attachment ) {
    echo wp_get_attachment_link( $id ) . "\n";
}

这样,您就可以获得播放列表中每个音频文件的附件ID和对象。因此,如果您愿意,可以获得更多创意,而不是致电wp_get_attachment_link(),而是考虑使用wp_get_attachment_url()。您可能还会发现查看wp_playlist_shortcode()的来源并提取更多内容很有帮助,因此您可以按照自己喜欢的方式调整内容。

答案 2 :(得分:0)

您可以直接遍历所有post_type 附件,其中post_mime_type音频

请参阅get_posts()的WordPress文档页面,了解可以传递给get_posts()函数的选项。

// Specify options here
$args = array (
    'post_type' => 'attachment',
    'post_mime_type' => 'audio',
    'numberposts' => -1
);

// This is now an array with all audio files
$audiofiles = get_posts($args);

foreach ($audiofiles as $file)
{   
     // Do what ever you want with it here ...
     // Eg.
     $url = wp_get_attachment_url($file->ID);
     echo $url; // url
     echo file->post_title; //title
     echo file->post_content; // description
}

如果您只想根据当前帖子检索音频文件。然后将post_parent添加到$args数组中。例如

'post_parent' => $post->ID // Which is the current post ID
相关问题