我们可以看看get_post_galleries()
了解如何处理播放列表而不是库。
以下是基于核心功能的未经测试的修改:
function wpse_get_post_playlists( $post, $html = true ) {
if ( ! $post = get_post( $post ) )
return array();
if ( ! has_shortcode( $post->post_content, \'playlist\' ) )
return array();
$playlists = array();
if ( preg_match_all( \'/\' . get_shortcode_regex() . \'/s\', $post->post_content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $shortcode ) {
if ( \'playlist\' === $shortcode[2] ) {
$ids = array();
$shortcode_attrs = shortcode_parse_atts( $shortcode[3] );
if ( ! is_array( $shortcode_attrs ) ) {
$shortcode_attrs = array();
}
// Specify the post id of the playlist we\'re viewing if the shortcode doesn\'t reference another post already.
if ( ! isset( $shortcode_attrs[\'id\'] ) ) {
$shortcode[3] .= \' id="\' . intval( $post->ID ) . \'"\';
}
$playlist = do_shortcode_tag( $shortcode );
if ( $html ) {
$playlists[] = $playlist;
} else {
preg_match_all( \'#ids=([\\\'"])(.+?)\\1#is\', $playlist, $ids, PREG_SET_ORDER );
if ( ! empty( $ids ) ) {
foreach ( $ids as $id ) {
$ids[] = $id[2];
}
}
$playlists[] = array_merge(
$shortcode_attrs,
array(
\'ids\' => array_values( array_unique( $ids ) )
)
);
}
}
}
}
return $playlists;
}