vendredi 30 janvier 2015

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


My reason for doing this is for a music & entertainment site where users will have the ability to share music and videos on other websites via embed code. I'm using the audio.php and video.php attachment pages to load the default MediaElement.js HTML5 Player with all the attached media from it's parent post. I then link to that attachment page via Iframe, thus resulting in a embed code for each audio and video post.


I'm sure a plugin would be much better solution but this is what I can do with my current skill level.


This is what I've gotten thus far.


For the playlist:



<?php
global $post;
$args = array(
'post_parent' => $post->post_parent,
'numberposts' => -1,
'post_status' => 'null',
'post_type' => 'attachment',
'post_mime_type' => array('audio'),
'order' => 'ASC' );

$attachments = get_posts($args);
$medias = $attachments;

if (count($medias) > 0) {
$array = array();
foreach($medias as $media) {
$array[] = $media->ID;
}

$id_comma_separated = implode(',', $array);
echo do_shortcode('[playlist ids="' . $id_comma_separated . '"]');
}
?>


And for single audio.



<?php
global $post;
$args = array(
'post_parent' => $post->post_parent,
'numberposts' => 1,
'post_status' => 'null',
'post_type' => 'attachment',
'post_mime_type' => array('audio'),
'order' => 'ASC' );

$attachments = get_posts($args);
$medias = $attachments;

$medias = wp_get_attachment_url( $attachment_id, 'full' );
echo do_shortcode('[audio src="' . $medias . '"]');
?>


Now obviously this always returns two players no matter what, I need to make this a single query that either executes the [audio] shortcode if only a single audio is available and [playlist] if there is more than one audio attachment in the parent post.


This is as far as I've gotten, everything I tried after this broke the audio attachment page.


I'm a designer not a developer so my PHP skills are very limited, so if there's a better way to do this whole thing, that would be great.


Thanks.





Aucun commentaire:

Enregistrer un commentaire