Audio or playlist shortcode condition according to the amount of files on attachment page

Some ideas for your attachments file:

A simplification:

$tmp   = $post;                          // Store the current global post object
$post  = get_post( $post->post_parent ); // Use the post parent post object
$media = get_attached_media( 'audio' );
if( $media )
{
    $sc = "";
    if( count( $media ) > 1 )
    {
        $ids = join( ',', wp_list_pluck( $media, 'ID' ) );
        $sc = "";      
    }
    echo do_shortcode( $sc );
}
$post = $tmp;                            // restore the right global $post object

where we use that the stripped shortcode, plays the current post’s attached audio file.

We can simplify it more:

$tmp  = $post;  
$post = get_post( $post->post_parent ); 
if( $media = get_attached_media( 'audio' ) )
{
    $sc = "";
    if( count( $media ) > 1 )
        $sc="";
    echo do_shortcode( $sc );
}
$post = $tmp; 

where the stripped shortcode, plays the current post’s attached audio files.

Even shorter version:

$tmp  = $post;  
$post = get_post( $post->post_parent );
if( $media = get_attached_media( 'audio' ) )            
    echo count( $media ) > 1 ? wp_playlist_shortcode() : wp_audio_shortcode();
$post = $tmp; 

where we use the audio and playlist shortcode callbacks directly.

… and finally:

$tmp  = $post;  
$post = get_post( $post->post_parent ); 
echo count( get_attached_media( 'audio' ) ) > 1 ? wp_playlist_shortcode() : wp_audio_shortcode();
$post = $tmp; 

Notice that we assume that we are in the loop.

Modify the attachment’s page through a filter:

If you want to append the audio/playlist player to the attachment’s content, you can try for example:

/**
 * Append the audio/playlist from the parent post, to the attachment's content.
 *
 * @see http://wordpress.stackexchange.com/a/176600/26350
 */

add_filter( 'the_content', function( $content )
{
    if( is_attachment() )
    {
        global $post;
        $tmp = $post;
        $post = get_post( $post->post_parent );
        $content .= count( get_attached_media( 'audio' ) ) > 1 ? wp_playlist_shortcode() : wp_audio_shortcode();
        $post = $tmp;
    }
    return $content;
});            

Leave a Comment