【PHP】如何從wordpress音訊播放列表中獲取單個歌曲?
2020-12-05 PHP
我正在嘗試從建立的播放列表中獲取歌曲名稱和URL。建立帖子後會動態生成播放列表。我將不得不遍歷這些帖子,並獲取要拆分的播放列表,並顯示名稱,持續時間等。
我已經搜尋過,但我只能找到這些播放列表的外掛。
我想從釋出的播放列表中拆分音訊檔案,並僅在網站的某些部分中顯示歌曲的名稱。
我可以將單個音訊檔案顯示為
$music_file = get_template_directory_uri() . "/sounds/music.mp3";
echo do_shortcode('[audio mp3=' . $music_file . ']');
但我想從播放列表中獲取所有音訊檔案,並逐個迴圈瀏覽,並建立播放列表的自定義顯示。
更新
例如,我可以使用
$gallery = get_post_gallery_images( $post )從相簿中獲取單個影象。是否有可以對音訊播放列表執行相同功能的功能。播放列表是通過編輯器中的媒體上傳建立的
解決辦法
儘管您的問題尚不清楚,但是此程式碼將幫助您在播放列表中找到每個音訊名稱/網址:
// 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) . ']');
}